Gate Cutting to Reduce Circuit Width

In this tutorial we will simulate the expectation value of a four-qubit circuit using only two-qubit experiments by cutting gates in the circuit.

Like any circuit knitting technique, gate cutting can be described as three consecutive steps:

  • cut some non-local gates in the circuit and possibly separate the circuit into subcircuits

  • execute many sampled subexperiments using the Qiskit Sampler primitive

  • reconstruct the expectation value of the full-sized circuit

Create a circuit to cut

[1]:
from qiskit.circuit.library import EfficientSU2

qc = EfficientSU2(4, entanglement="linear", reps=2).decompose()
qc.assign_parameters([0.4] * len(qc.parameters), inplace=True)

qc.draw("mpl", scale=0.8)
[1]:
../../_images/circuit_cutting_tutorials_01_gate_cutting_to_reduce_circuit_width_2_0.png

Specify an observable

[2]:
from qiskit.quantum_info import SparsePauliOp

observable = SparsePauliOp(["ZZII", "IZZI", "-IIZZ", "XIXI", "ZIZZ", "IXIX"])

Separate the circuit and observable according to a specified qubit partitioning

Each label in partition_labels corresponds to the circuit qubit in the same index. Qubits sharing a common partition label will be grouped together, and non-local gates spanning more than one partition will be cut.

Note: The observables kwarg to partition_problem is of type PauliList. Observable term coefficients and phases are ignored during decomposition of the problem and execution of the subexperiments. They may be re-applied during reconstruction of the expectation value.

[3]:
from circuit_knitting.cutting import partition_problem

partitioned_problem = partition_problem(
    circuit=qc, partition_labels="AABB", observables=observable.paulis
)
subcircuits = partitioned_problem.subcircuits
subobservables = partitioned_problem.subobservables
bases = partitioned_problem.bases

Visualize the decomposed problem

[4]:
subobservables
[4]:
{'A': PauliList(['II', 'ZI', 'ZZ', 'XI', 'ZZ', 'IX']),
 'B': PauliList(['ZZ', 'IZ', 'II', 'XI', 'ZI', 'IX'])}
[5]:
subcircuits["A"].draw("mpl", scale=0.8)
[5]:
../../_images/circuit_cutting_tutorials_01_gate_cutting_to_reduce_circuit_width_9_0.png
[6]:
subcircuits["B"].draw("mpl", scale=0.8)
[6]:
../../_images/circuit_cutting_tutorials_01_gate_cutting_to_reduce_circuit_width_10_0.png

Calculate the sampling overhead for the chosen cuts

Here we cut two CNOT gates, resulting in a sampling overhead of \(9^2\).

For more on the sampling overhead incurred by circuit cutting, refer to the explanatory material.

[7]:
import numpy as np

print(f"Sampling overhead: {np.prod([basis.overhead for basis in bases])}")
Sampling overhead: 81.0

Generate the subexperiments to run on the backend

generate_cutting_experiments accepts circuits/observables args as dictionaries mapping qubit partition labels to the respective subcircuit/subobservables.

To simulate the expectation value of the full-sized circuit, many subexperiments are generated from the decomposed gates’ joint quasiprobability distribution and then executed on one or more backends. The number of samples taken from the distribution is controlled by num_samples, and one combined coefficient is given for each unique sample. For more information on how the coefficients are calculated, refer to the explanatory material.

[8]:
from circuit_knitting.cutting import generate_cutting_experiments

subexperiments, coefficients = generate_cutting_experiments(
    circuits=subcircuits, observables=subobservables, num_samples=np.inf
)

Choose a backend

Here we are using a fake backend, which will result in Qiskit Runtime running in local mode (i.e., on a local simulator).

[9]:
from qiskit_ibm_runtime.fake_provider import FakeManilaV2

backend = FakeManilaV2()

Prepare the subexperiments for the backend

We must transpile the circuits with our backend as the target before submitting them to Qiskit Runtime.

[10]:
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

# Transpile the subexperiments to ISA circuits
pass_manager = generate_preset_pass_manager(optimization_level=1, backend=backend)
isa_subexperiments = {
    label: pass_manager.run(partition_subexpts)
    for label, partition_subexpts in subexperiments.items()
}

Run the subexperiments using the Qiskit Runtime Sampler primitive

[11]:
from qiskit_ibm_runtime import SamplerV2, Batch

# Set up a Qiskit Runtime Sampler primitive for each circuit partition
samplers = {label: SamplerV2(backend=backend) for label in subexperiments.keys()}

# Submit each partition's subexperiments as a single batch
with Batch(backend=backend):
    jobs = {
        label: sampler.run(isa_subexperiments[label], shots=2**12)
        for label, sampler in samplers.items()
    }

# Retrive results
results = {label: job.result() for label, job in jobs.items()}
/home/runner/work/circuit-knitting-toolbox/circuit-knitting-toolbox/.tox/docs/lib/python3.9/site-packages/qiskit_ibm_runtime/session.py:157: UserWarning: Session is not supported in local testing mode or when using a simulator.
  warnings.warn(

Reconstruct the expectation values

Reconstruct expectation values for each observable term and combine them to reconstruct the expectation value for the original observable.

[12]:
from circuit_knitting.cutting import reconstruct_expectation_values

# Get expectation values for each observable term
reconstructed_expvals = reconstruct_expectation_values(
    results,
    coefficients,
    subobservables,
)

# Reconstruct final expectation value
final_expval = np.dot(reconstructed_expvals, observable.coeffs)

Compare the reconstructed expectation values with the exact expectation value from the original circuit and observable

[13]:
from qiskit_aer.primitives import EstimatorV2

estimator = EstimatorV2()
exact_expval = estimator.run([(qc, observable)]).result()[0].data.evs
print(f"Reconstructed expectation value: {np.real(np.round(final_expval, 8))}")
print(f"Exact expectation value: {np.round(exact_expval, 8)}")
print(f"Error in estimation: {np.real(np.round(final_expval-exact_expval, 8))}")
print(
    f"Relative error in estimation: {np.real(np.round((final_expval-exact_expval) / exact_expval, 8))}"
)
Reconstructed expectation value: 0.73700875
Exact expectation value: 0.56254612
Error in estimation: 0.17446263
Relative error in estimation: 0.31013035