QCut Basic Usage

QCut Basic Usage#

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.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/ab6c0c7c179bf2955afa940349bb3a1af3b364d7915cb4547ff9f859c2b95721.png
#insert cut

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

cut_circuit.draw("mpl")
../_images/dc8a0e58926253b22d9679f0a078260a43de86aa0e0ca96e08e8a5731e7b69ec.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/69d0280b434a64af01b471f82ad7c04d3a50fd93cb9ac8c6420533edff2e4af1.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)
#define observables to calculate expectation values for
observables = [0,1,2, [0,2]]

#get the approximated expectation values
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, 3)

#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.005548 0.005548 0.009522 0.998279]
Exact expectation values with ideal simulator :[0.000000 0.000000 0.000000 1.000000]