Measuring specific qubits with OpenQASM 3.0
The local state vector simulator and local density matrix simulator support submitting OpenQASM programs where a subset of the circuit's qubits can be measured. This is often called partial measurement. For example, in the following code you can create a two-qubit circuit and only measure the first qubit.
partial_measure_qasm = """ OPENQASM 3.0; bit[1] b; qubit[2] q; h q[0]; cnot q[0], q[1]; b[0] = measure q[0]; """
There are two qubits, q[0]
and q[1]
but we are only measuring qubit 0 here: b[0] = measure q[0]
.
Now, run the following on the local state vector simulator.
from braket.devices import LocalSimulator local_sim = LocalSimulator() partial_measure_local_sim_task = local_sim.run(OpenQASMProgram(source=partial_measure_qasm), shots = 10) partial_measure_local_sim_result = partial_measure_local_sim_task.result() print(partial_measure_local_sim_result.measurement_counts) print("Measured qubits: ", partial_measure_local_sim_result.measured_qubits)
You can check whether a device supports partial measurement by inspecting the requiresAllQubitsMeasurement
field
in its action properties; if it is False
, then partial measurement is supported.
AwsDevice(Devices.Rigetti.AspenM3).properties.action['braket.ir.openqasm.program'].requiresAllQubitsMeasurement
Here, requiresAllQubitsMeasurement
is False
, which indicates that not all qubits must be measured.