Execute dynamic circuits
Halaman ini belum diterjemahkan. Anda sedang melihat versi asal dalam bahasa Inggeris.
Package versions
The code on this page was developed using the following requirements. We recommend using these versions or newer.
qiskit[all]~=2.4.0
qiskit-ibm-runtime~=0.46.1
Dynamic circuits are powerful tools with which you can measure qubits in the middle of a quantum circuit execution and then perform classical logic operations within the circuit, based on the outcome of those mid-circuit measurements. This process is also known as classical feedforward. While these are early days of understanding how best to take advantage of dynamic circuits, the quantum research community has already identified a number of use cases, such as the following:
- Efficient quantum state preparation, such as GHZ state, W-state (for more information about W-state, also refer to "State preparation by shallow circuits using feed forward"), and a broad class of matrix product states
- Efficient long-range entanglement between qubits on the same chip by using shallow circuits
- Efficient sampling of IQP-like circuits
These improvements brought by dynamic circuits, however, come with trade-offs. Mid-circuit measurements and classical operations typically have longer execution time than two-qubit gates, and this increase in time might negate the benefits of reduced circuit depth. Therefore, reducing the length of mid-circuit measurement is a focus area of improvement as IBM Quantumยฎ releases the new version of dynamic circuits. For other restrictions when using dynamic circuits, see the Estimator or Sampler Feature compatibility table.
The OpenQASM 3 specification defines a number of control-flow structures, but Qiskit Runtime currently only supports the conditional if statement. In Qiskit SDK, this corresponds to the if_test method on QuantumCircuit. This method returns a context manager and is typically used in a with statement. This guide describes how to use this conditional statement.
The code examples in this guide use the standard measure instruction for mid-circuit measurements. However, it is recommended that you use the MidCircuitMeasure instruction instead, if the backend supports it. See the Mid-circuit measurements section for details.
Find backends that support dynamic circuitsโ
To find all backends that your account can access and support dynamic circuits, run code like the following. This example assumes that you have saved your login credentials. You could also explicitly specify credentials when initializing your Qiskit Runtime service account. This would let you view backends available on a specific instance or plan type, for example.
- The backends that are available to the account depend on the instance specified in the credentials.
- The new version of dynamic circuits is now available to all users on all backends. See the announcement for more details.
# Added by doQumentation โ required packages for this notebook
!pip install -q qiskit qiskit-ibm-runtime
# This cell is hidden from users. It hides all those "...instance was not set..." warnings.
import warnings
warnings.filterwarnings("ignore", message=".*Instance was not set*")
from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()
dc_backends = service.backends(dynamic_circuits=True)
print(dc_backends)
[<IBMBackend('ibm_boston')>, <IBMBackend('ibm_pittsburgh')>, <IBMBackend('ibm_fez')>, <IBMBackend('ibm_marrakesh')>, <IBMBackend('ibm_kingston')>]
Mid-circuit measurementsโ
Prior to qiskit-ibm-runtime v0.43.0, measure was the only measurement instruction in Qiskit. Mid-circuit measurements, however, have different tuning requirements than terminal measurements (measurements that happen at the end of a circuit). For example, you need to consider the instruction duration when tuning a mid-circuit measurement because longer instructions cause noisier circuits. You don't need to consider instruction duration for terminal measurements because there are no instructions after terminal measurements.
The MidCircuitMeasure instruction maps to the measure_2 instruction reported in the backend's supported_instructions. However, measure_2 is not supported on all backends. Use service.backends(filters=lambda b: "measure_2" in b.supported_instructions) to find backends that support it. New measurements might be added in the future, but this is not guarenteed.
MidCircuitMeasure methodโ
In qiskit-ibm-runtime v0.43.0, the MidCircuitMeasure instruction was introduced. As the name suggests, it is a new measurement instruction that is optimized for mid-circuit on IBMยฎ QPUs. While you can use QuantumCircuit.measure for a mid-circuit measurement, because of its design, MidCircuitMeasure is typically a better choice. For example, it adds less overhead to your circuit than when using QuantumCircuit.measure.
from qiskit import QuantumCircuit
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_runtime.circuit import MidCircuitMeasure
service = QiskitRuntimeService()
backend = service.least_busy(
operational=True, simulator=False, dynamic_circuits=True
)
circ = QuantumCircuit(2, 2)
circ.x(0)
circ.append(MidCircuitMeasure(), [0], [0])
# circ.measure([0], [0])
# circ.measure_all()
print(circ.draw(cregbundle=False))