QCut: Multiple cuts on single wire

QCut: Multiple cuts on single wire#

import QCut as ck
from QCut import cut_wire
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit_aer.primitives import Estimator
#define initial circuit

circuit = QuantumCircuit(3)
circuit.h(0)
circuit.cx(0,1)
circuit.cx(1,2)
circuit.cx(0,1)
circuit.measure_all()

circuit.draw("mpl")
c:\Users\nivalajo\AppData\Local\miniconda3\envs\testenv\lib\site-packages\qiskit\visualization\circuit\matplotlib.py:266: FutureWarning: The default matplotlib drawer scheme will be changed to "iqp" in a following release. To silence this warning, specify the current default explicitly as style="clifford", or the new default as style="iqp".
  self._style, def_font_ratio = load_style(self._style)
../_images/3f4f4c989175c6b17c353b43eb847d865cef3044bae5fbd1f9ba4972d1231edf.png
#insert cuts

cut_circuit = QuantumCircuit(5)
cut_circuit.h(0)
cut_circuit.cx(0,1)
cut_circuit.append(cut_wire, [1,3])
cut_circuit.cx(3,4)
cut_circuit.append(cut_wire, [3,2])
cut_circuit.cx(0,2)

cut_circuit.draw("mpl")
../_images/290ea04f671a731e93423d6ace4da3e41f1bac9ea7d915a5ec780dd7922fcd2f.png
#find cut locations and separate into subcircuits

cut_locations, subcircuits = ck.get_locations_and_subcircuits(cut_circuit)
subcircuits[0].draw("mpl")
../_images/9233e2e89e8eaf71517d26b5e5b54d88deffeb56564814346548bbd137968bac.png
subcircuits[1].draw("mpl")
../_images/8f13bdba14082e44cc495dbdb5f40fd9f51f7be0be62301db476490c6f997b4c.png
#insert qpd operations to get the 8^n experiment circuits, where n is the number of cuts made
#also returns coefficients used for reconstructing the expectation values
#and locations of identity basis measurements that get added with post processing

experiment_circuits, coefficients, id_meas = ck.get_experiment_circuits(subcircuits, cut_locations)
#define backend
backend = AerSimulator()
#run experiment circuits
#run_experiments() also post processes the results

results = ck.run_experiments(experiment_circuits, cut_locations, id_meas, backend=backend, mitigate=False)
#define observables to calculate expectation values for
observables = [0,1,2, [0,2]]

#get the exact expectation values with ideal simulator
expectation_values = ck.estimate_expectation_values(results, coefficients, cut_locations, observables)
#transform observables into a PauliList for the Qiskit Estimator
paulilist_observables = ck.get_pauli_list(observables, circuit.num_qubits)

#get the exact expectation values with ideal simulator
estimator = Estimator(run_options={"shots": None}, approximation=True)
exact_expvals = (
    estimator.run([circuit] * len(paulilist_observables),  # noqa: PD011
                  list(paulilist_observables)).result().values
)
import numpy as np

#set numpy print options
np.set_printoptions(formatter={"float": lambda x: f"{x:0.6f}"})
expectation_values = np.array(expectation_values).astype(float)

#compare QCut to Estimator
print(f"QCut circuit knitting expectation values:{np.array(expectation_values)}")
print(f"Exact expectation values with ideal simulator :{np.array(exact_expvals)}")
QCut circuit knitting expectation values:[-0.001299 1.000582 -0.029390 0.987560]
Exact expectation values with ideal simulator :[0.000000 1.000000 0.000000 1.000000]