Wire Cutting Phrased as a Two-Qubit Move
Instruction¶
In this tutorial, we will reconstruct expectation values of a seven-qubit circuit by splitting it into two four-qubit circuits using wire cutting.
Like any circuit knitting technique, wire cutting can be described as three consecutive steps:
cut some wires 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¶
First, we begin with a circuit inspired by Fig. 1(a) of arXiv:2302.03366v1.
[1]:
import numpy as np
from qiskit import QuantumCircuit
qc_0 = QuantumCircuit(7)
for i in range(7):
qc_0.rx(np.pi / 4, i)
qc_0.cx(0, 3)
qc_0.cx(1, 3)
qc_0.cx(2, 3)
qc_0.cx(3, 4)
qc_0.cx(3, 5)
qc_0.cx(3, 6)
qc_0.cx(0, 3)
qc_0.cx(1, 3)
qc_0.cx(2, 3)
[1]:
<qiskit.circuit.instructionset.InstructionSet at 0x7f8e68e1aaf0>
[2]:
qc_0.draw("mpl")
[2]:
Specify an observable¶
[3]:
from qiskit.quantum_info import SparsePauliOp
observable = SparsePauliOp(["ZIIIIII", "IIIZIII", "IIIIIIZ"])
Create a new circuit where Move
instructions have been placed at the desired cut locations¶
Given the above circuit, we would like to place two wire cuts on the middle qubit line, so that the circuit can separate into two circuits of four qubits each. One way to do this is to manually place two-qubit Move
instructions that move the state from one qubit wire to another. A Move
instruction is conceptually equivalent to a reset operation on the second qubit, followed by a SWAP gate. The effect of this instruction is to transfer the state of the first (source) qubit to the second
(detination) qubit, while discarding the incoming state of the second qubit. For this to work as intended, it is important that the second (destination) qubit share no entanglement with the remainder of the system; otherwise, the reset operation will cause the state of the remainder of the system to be partially collapsed.
Here, we build a new circuit with one additional qubit and the Move
operations in place. In this example, we are able to reuse a qubit: the source qubit of the first Move
becomes the destination qubit of the second Move
operation.
Note: As an alternative to working directly with Move
instructions, one may choose to mark wire cuts using a single-qubit CutWire
instruction. The cut_wires
function exists to transform CutWire
s to Move
instructions on newly allocated qubits. However, in contrast to the manual method, this automatic method does not allow for the re-use of qubit wires. See the CutWire
how-to guide for details.
[4]:
from circuit_knitting.cutting.instructions import Move
qc_1 = QuantumCircuit(8)
for i in [*range(4), *range(5, 8)]:
qc_1.rx(np.pi / 4, i)
qc_1.cx(0, 3)
qc_1.cx(1, 3)
qc_1.cx(2, 3)
qc_1.append(Move(), [3, 4])
qc_1.cx(4, 5)
qc_1.cx(4, 6)
qc_1.cx(4, 7)
qc_1.append(Move(), [4, 3])
qc_1.cx(0, 3)
qc_1.cx(1, 3)
qc_1.cx(2, 3)
qc_1.draw("mpl")
[4]:
Create observable to go with the new circuit¶
This observable corresponds with observable
, but we must account correctly for the extra qubit wire that has been added (i.e., we insert an “I” at index 4). Note that in Qiskit, the string representation qubit-0 corresponds to the right-most Pauli character.
[5]:
observable_expanded = SparsePauliOp(["ZIIIIIII", "IIIIZIII", "IIIIIIIZ"])
Separate the circuit and observables¶
As in the previous tutorials, qubits sharing a common partition label will be grouped together, and non-local gates spanning more than one partition will be cut.
[6]:
from circuit_knitting.cutting import partition_problem
partitioned_problem = partition_problem(
circuit=qc_1, partition_labels="AAAABBBB", observables=observable_expanded.paulis
)
subcircuits = partitioned_problem.subcircuits
subobservables = partitioned_problem.subobservables
bases = partitioned_problem.bases
Visualize the decomposed problem¶
[7]:
subobservables
[7]:
{'A': PauliList(['IIII', 'ZIII', 'IIIZ']),
'B': PauliList(['ZIII', 'IIII', 'IIII'])}
[8]:
subcircuits["A"].draw("mpl")
[8]:
[9]:
subcircuits["B"].draw("mpl")
[9]:
Calculate the sampling overhead for the chosen cuts¶
Here we cut two wires, resulting in a sampling overhead of \(4^4\).
For more on the sampling overhead incurred by circuit cutting, refer to the explanatory material.
[10]:
print(f"Sampling overhead: {np.prod([basis.overhead for basis in bases])}")
Sampling overhead: 256.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.
[11]:
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).
[12]:
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.
[13]:
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¶
[14]:
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(
To use the Qiskit Runtime Sampler, replace the code above with this commented block.
Reconstruct the expectation values¶
Reconstruct expectation values for each observable term and combine them to reconstruct the expectation value for the original observable.
[15]:
from circuit_knitting.cutting import reconstruct_expectation_values
reconstructed_expvals = reconstruct_expectation_values(
results,
coefficients,
subobservables,
)
final_expval = np.dot(reconstructed_expvals, observable.coeffs)
Compare the reconstructed expectation values with the exact expectation value from the original circuit and observable¶
[16]:
from qiskit_aer.primitives import EstimatorV2
estimator = EstimatorV2()
exact_expval = estimator.run([(qc_0, 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: 1.51857895
Exact expectation value: 1.59099026
Error in estimation: -0.07241131
Relative error in estimation: -0.04551336