From 2cc007bdbdebf6b660f0a8436bd76e3671d7f869 Mon Sep 17 00:00:00 2001 From: Rebecca Dimock <66339736+beckykd@users.noreply.github.com> Date: Fri, 4 Oct 2024 08:36:18 -0500 Subject: [PATCH] add measurement information (#1988) Closes #1925 --------- Co-authored-by: Julien Gacon Co-authored-by: ABBY CROSS --- docs/guides/_toc.json | 4 + docs/guides/map-problem-to-circuits.mdx | 1 + docs/guides/measure-qubits.mdx | 96 +++++++++++++++++++++++ qiskit_bot.yaml | 2 + scripts/js/commands/checkPatternsIndex.ts | 1 + 5 files changed, 104 insertions(+) create mode 100644 docs/guides/measure-qubits.mdx diff --git a/docs/guides/_toc.json b/docs/guides/_toc.json index 6fe1d0de5b2..7b545df4de3 100644 --- a/docs/guides/_toc.json +++ b/docs/guides/_toc.json @@ -137,6 +137,10 @@ "title": "Classical feedforward and control flow", "url": "/guides/classical-feedforward-and-control-flow" }, + { + "title": "Measure qubits", + "url": "/guides/measure-qubits" + }, { "title": "Synthesize unitary operators", "url": "/guides/synthesize-unitary-operators" diff --git a/docs/guides/map-problem-to-circuits.mdx b/docs/guides/map-problem-to-circuits.mdx index 066fb8b9f2f..0c50694b192 100644 --- a/docs/guides/map-problem-to-circuits.mdx +++ b/docs/guides/map-problem-to-circuits.mdx @@ -28,6 +28,7 @@ The output of this step in a Qiskit pattern is normally a collection of circuits ### Build circuits with the Qiskit SDK * [Circuit library](./circuit-library) * [Construct circuits](./construct-circuits) +* [Measure qubits](./measure-qubits) * [Visualize circuits](./visualize-circuits) * [Classical feedforward and control flow](./classical-feedforward-and-control-flow) * [Synthesize unitary operators](./synthesize-unitary-operators) diff --git a/docs/guides/measure-qubits.mdx b/docs/guides/measure-qubits.mdx new file mode 100644 index 00000000000..9679d942478 --- /dev/null +++ b/docs/guides/measure-qubits.mdx @@ -0,0 +1,96 @@ +--- +title: Measure qubits +description: Learn how to measure qubits, including constraints on where measurements can be used. +--- + +# Measure qubits + +To get information about a qubit's state, you can _measure_ it onto a [classical bit](/api/qiskit/circuit#qiskit.circuit.Clbit). In Qiskit, measurements are performed in the computational basis, that is, the single-qubit Pauli-$Z$ basis. Therefore, a measurement yields 0 or 1, depending on the overlap with the Pauli-$Z$ eigenstates $|0\rangle$ and $|1\rangle$: + +$$ +|q\rangle \xrightarrow{measure}\begin{cases} + 0 (\text{outcome}+1), \text{with probability } p_0=|\langle q|0\rangle|^{2}\text{,} \\ + 1 (\text{outcome}-1), \text{with probability } p_1=|\langle q|1\rangle|^{2}\text{.} + \end{cases} +$$ + +## Apply a measurement to a circuit + +There are several ways to apply measurements to a circuit: + +### `QuantumCircuit.measure` method + +Use the [`measure`](/api/qiskit/qiskit.circuit.QuantumCircuit#measure) method to measure a [`QuantumCircuit`](/api/qiskit/qiskit.circuit.QuantumCircuit#quantumcircuit-class). + +Examples: + +```python +from qiskit import QuantumCircuit + +qc = QuantumCircuit(5, 5) +qc.x(0) +qc.x(1) +qc.x(4) +qc.measure(range(5), range(5)) # Measures all qubits into the corresponding clbit. +``` + +```python +from qiskit import QuantumCircuit + +qc = QuantumCircuit(3, 1) +qc.x([0, 2]) +qc.measure(1, 0) # Measure qubit 1 into the classical bit 0. +``` + +### `Measure` class + +The Qiskit [Measure](/api/qiskit/circuit#qiskit.circuit.Measure) class measures the specified qubits. + +```python +from qiskit.circuit import Measure + +.... +qc.append(Measure(), [0], [0]) # measure qubit 0 into clbit 0 +``` + +### `QuantumCircuit.measure_all` method + +To measure all qubits into the corresponding classical bits, use the [`measure_all`](/api/qiskit/qiskit.circuit.QuantumCircuit#measure_all) method. By default, this method adds new classical bits in a `ClassicalRegister` to store these measurements. + +```python +from qiskit import QuantumCircuit + +qc = QuantumCircuit(3, 1) +qc.x([0, 2]) +qc.measure_all() # Measure all qubits. +``` + +### `QuantumCircuit.measure_active` method + +To measure all qubits that are not idle, use the [`measure_active`](/api/qiskit/qiskit.circuit.QuantumCircuit#measure_active) method. This method creates a new `ClassicalRegister` with a size equal to the number of non-idle qubits being measured. + +```python +from qiskit import QuantumCircuit + +qc = QuantumCircuit(3, 1) +qc.x([0, 2]) +qc.measure_active() # Measure qubits that are not idle, i.e., qubits 0 and 2. +``` + + + + +* Circuits that contain operations _after_ a measurement are called dynamic circuits. Not all QPUs or simulators support these. +* There must be at least one classical register in order to use measurements. +* The Sampler primitive requires circuit measurements. You can add circuit measurements with the Estimator primitive, but they are ignored. + + + +## Next steps + + +- [`Measure`](/api/qiskit/circuit#qiskit.circuit.Measure) class +- [`measure_all`](/api/qiskit/qiskit.circuit.QuantumCircuit#measure_all) method +- [`measure_active`](/api/qiskit/qiskit.circuit.QuantumCircuit#measure_active) method +- [`random_circuit`](/api/qiskit/circuit#random_circuit) method + \ No newline at end of file diff --git a/qiskit_bot.yaml b/qiskit_bot.yaml index bee0b88ad8b..8501d25f8d0 100644 --- a/qiskit_bot.yaml +++ b/qiskit_bot.yaml @@ -73,6 +73,8 @@ notifications: - "`@lerongil`" - "@jyu00" - "@beckykd" + "docs/guides/measure-qubits": + - "@beckykd" "docs/guides/get-qpu-information": - "@frankharkins" - "@abbycross" diff --git a/scripts/js/commands/checkPatternsIndex.ts b/scripts/js/commands/checkPatternsIndex.ts index d436964a4b1..8fac25215f2 100644 --- a/scripts/js/commands/checkPatternsIndex.ts +++ b/scripts/js/commands/checkPatternsIndex.ts @@ -36,6 +36,7 @@ const ALLOWLIST_MISSING_FROM_TOC: Set = new Set([ "/guides/post-process-results", "/guides/q-ctrl-optimization-solver", "/guides/qunasys-quri-chemistry", + "/guides/circuit-library", ]); const INDEX_PAGES = [