QCut to Three Parts

QCut to Three Parts#

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(4)
circuit.h(0)
circuit.cx(0,1)
circuit.cx(1,2)
circuit.cx(2,3)
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/25af6103c8b0a4603cad1e095eb7bf9f2704fda6f719d6aa2e99a3b316aea95b.png
#insert cuts

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

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

cut_locations, subcircuits = ck.get_locations_and_subcircuits(cut_circuit)
subcircuits[0].draw("mpl")
../_images/6842a32206353991157c1b86040ac9c042afddb6c6397fe0ae3ddafbe62189ba.png
subcircuits[1].draw("mpl")
../_images/eca58ef5bdafd123604c71048bd3dc6a645e670a94de8eb4fe507bcfdf40bfcc.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,3, [0,2], [0,1,3]]

#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.002290 0.002290 0.012985 -0.018276 0.986128 -0.018276]
Exact expectation values with ideal simulator :[0.000000 0.000000 0.000000 0.000000 1.000000 0.000000]