231 lines
6.7 KiB
Plaintext
231 lines
6.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "107b5a9b-d0d1-47d2-84fe-de1fe20bc954",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Measure qubits"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c15b4675-bf2d-4104-8cd0-46038230dfc7",
|
|
"metadata": {
|
|
"tags": [
|
|
"version-info"
|
|
]
|
|
},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4ead9330-71f7-47f2-9056-03c13ead496b",
|
|
"metadata": {},
|
|
"source": [
|
|
"To get information about a qubit's state, you can _measure_ it onto a [classical bit](/docs/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$:\n",
|
|
"\n",
|
|
"$$\n",
|
|
"|q\\rangle \\xrightarrow{measure}\\begin{cases}\n",
|
|
" 0 (\\text{outcome}+1), \\text{with probability } p_0=|\\langle q|0\\rangle|^{2}\\text{,} \\\\\n",
|
|
" 1 (\\text{outcome}-1), \\text{with probability } p_1=|\\langle q|1\\rangle|^{2}\\text{.}\n",
|
|
" \\end{cases}\n",
|
|
"$$\n",
|
|
"\n",
|
|
"## Apply a measurement to a circuit\n",
|
|
"\n",
|
|
"There are several ways to apply measurements to a circuit:\n",
|
|
"\n",
|
|
"### `QuantumCircuit.measure` method\n",
|
|
"\n",
|
|
"Use the [`measure`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#measure) method to measure a [`QuantumCircuit`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#quantumcircuit-class).\n",
|
|
"\n",
|
|
"Examples:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "0aab2e6c-1bdb-44f0-a34b-127bd34f4fde",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"<qiskit.circuit.instructionset.InstructionSet at 0x7f61dc3e75b0>"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from qiskit import QuantumCircuit\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(5, 5)\n",
|
|
"qc.x(0)\n",
|
|
"qc.x(1)\n",
|
|
"qc.x(4)\n",
|
|
"qc.measure(\n",
|
|
" range(5), range(5)\n",
|
|
") # Measures all qubits into the corresponding clbit."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "173b07f8-4859-4b07-9c3e-b2187021e25a",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"<qiskit.circuit.instructionset.InstructionSet at 0x7f622841ab60>"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from qiskit import QuantumCircuit\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(3, 1)\n",
|
|
"qc.x([0, 2])\n",
|
|
"qc.measure(1, 0) # Measure qubit 1 into the classical bit 0."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d4180041-d976-48ef-b01e-51150888afdb",
|
|
"metadata": {},
|
|
"source": [
|
|
"### `Measure` class\n",
|
|
"\n",
|
|
"The Qiskit [Measure](/docs/api/qiskit/circuit#qiskit.circuit.Measure) class measures the specified qubits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "c12c0add-87d6-46e9-822b-adbc8f8e1ac7",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"<qiskit.circuit.instructionset.InstructionSet at 0x7f61dbadd420>"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from qiskit.circuit import Measure\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(3, 1)\n",
|
|
"qc.x([0, 1])\n",
|
|
"qc.append(Measure(), [0], [0]) # measure qubit 0 into clbit 0"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "4b2317cf-553b-424b-8a0a-c6d9649d6a7f",
|
|
"metadata": {},
|
|
"source": [
|
|
"### `QuantumCircuit.measure_all` method\n",
|
|
"\n",
|
|
"To measure all qubits into the corresponding classical bits, use the [`measure_all`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#measure_all) method. By default, this method adds new classical bits in a `ClassicalRegister` to store these measurements."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "1d630b08-9756-4192-aa94-9e45bcf5979e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit import QuantumCircuit\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(3, 1)\n",
|
|
"qc.x([0, 2])\n",
|
|
"qc.measure_all() # Measure all qubits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c0537010-0755-4f83-83bb-1cd5e07db0ad",
|
|
"metadata": {},
|
|
"source": [
|
|
"### `QuantumCircuit.measure_active` method\n",
|
|
"\n",
|
|
"To measure all qubits that are not idle, use the [`measure_active`](/docs/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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "79c8b510-00df-4442-9e8b-e7f7e2006cd7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit import QuantumCircuit\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(3, 1)\n",
|
|
"qc.x([0, 2])\n",
|
|
"qc.measure_active() # Measure qubits that are not idle, i.e., qubits 0 and 2."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "242ffc90-8db9-4e09-98d8-adb74b57afac",
|
|
"metadata": {},
|
|
"source": [
|
|
"<Admonition type=\"attention\" title=\"Important notes\">\n",
|
|
"\n",
|
|
"* Circuits that contain operations _after_ a measurement are called dynamic circuits. Not all QPUs or simulators support these.\n",
|
|
"* There must be at least one classical register in order to use measurements.\n",
|
|
"* The Sampler primitive requires circuit measurements. You can add circuit measurements with the Estimator primitive, but they are ignored.\n",
|
|
"\n",
|
|
"</Admonition>\n",
|
|
"\n",
|
|
"## Next steps\n",
|
|
"\n",
|
|
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
|
|
"- [`Measure`](/docs/api/qiskit/circuit#qiskit.circuit.Measure) class\n",
|
|
"- [`measure_all`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#measure_all) method\n",
|
|
"- [`measure_active`](/docs/api/qiskit/qiskit.circuit.QuantumCircuit#measure_active) method\n",
|
|
"- [`random_circuit`](/docs/api/qiskit/circuit#random_circuit) method\n",
|
|
"</Admonition>"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"description": "Learn how to measure qubits, including constraints on where measurements can be used.",
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3"
|
|
},
|
|
"title": "Measure qubits"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|