qiskit-documentation/docs/guides/specify-observables-pauli.i...

294 lines
27 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "212400e8-9fb9-4969-982c-8e13b369ef3d",
"metadata": {},
"source": [
"# Specify observables in the Pauli basis"
]
},
{
"cell_type": "markdown",
"id": "15c3654c-0235-44c4-9223-2251c24ef060",
"metadata": {
"tags": [
"version-info"
]
},
"source": [
"<details>\n",
"<summary><b>Package versions</b></summary>\n",
"\n",
"The code on this page was developed using the following requirements.\n",
"We recommend using these versions or newer.\n",
"\n",
"```\n",
"qiskit[all]~=1.2.4\n",
"qiskit-aer~=0.15.1\n",
"qiskit-ibm-runtime~=0.31.0\n",
"qiskit-serverless~=0.17.1\n",
"qiskit-ibm-catalog~=0.1\n",
"```\n",
"</details>"
]
},
{
"cell_type": "markdown",
"id": "a9c68a4e-189c-424b-9255-24226307c8ac",
"metadata": {},
"source": [
"In quantum mechanics, observables correspond to physical properties that can be measured.\n",
"When considering a system of spins, for example, you could be interested in measuring the system's energy or obtaining information about the alignment of the spins, such as the magnetization or the correlations between spins.\n",
"\n",
"To measure an $n$-qubit observable $O$ on a quantum computer, you must represent it as a sum of tensor products of Pauli operators, that is\n",
"\n",
"$$\n",
"O = \\sum_{k=1}^K \\alpha_k P_k,~~ P_k \\in \\{I, X, Y, Z\\}^{\\otimes n},~~ \\alpha_k \\in \\mathbb{R},\n",
"$$\n",
"\n",
"where\n",
"\n",
"$$\n",
"I = \\begin{pmatrix}\n",
"1 & 0 \\\\ 0 & 1\n",
"\\end{pmatrix}\n",
"~~\n",
"X = \\begin{pmatrix}\n",
"0 & 1 \\\\ 1 & 0\n",
"\\end{pmatrix}\n",
"~~\n",
"Y = \\begin{pmatrix}\n",
"0 & -i \\\\ i & 0\n",
"\\end{pmatrix}\n",
"~~\n",
"Z = \\begin{pmatrix}\n",
"1 & 0 \\\\ 0 & -1\n",
"\\end{pmatrix}\n",
"$$\n",
"\n",
"and you use the fact that an observable is Hermitian, as in, $O^\\dagger = O$. If $O$ is not Hermitian it can still be decomposed as a sum of Paulis, but the coefficient $\\alpha_k$ becomes complex.\n",
"\n",
"In many cases, the observable is naturally specified in this representation after mapping the system of interest to qubits.\n",
"For example, a spin-1/2 system can be mapped to an Ising Hamiltonian\n",
"\n",
"$$\n",
"H = \\sum_{\\langle i, j\\rangle} Z_i Z_j - \\sum_{i=1}^n X_i,\n",
"$$\n",
"\n",
"where the indices $\\langle i, j\\rangle$ run over interacting spins and the spins are subject to a transversal field in $X$.\n",
"The subscript index indicates which qubit the Pauli operator acts on, i.e. $X_i$ applies an $X$ operator on qubit $i$ and leaves the rest unchanged.\n",
"\n",
"In the Qiskit SDK, this Hamiltonian could be constructed with the following code."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7a06c7c4-582d-47ef-951e-52b40c1bfffb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SparsePauliOp(['IIIIIIIIIIZZ', 'IIIIIIIIIZZI', 'IIIIIIIIZZII', 'IIIIIIIZZIII', 'IIIIIIZZIIII', 'IIIIIZZIIIII', 'IIIIZZIIIIII', 'IIIZZIIIIIII', 'IIZZIIIIIIII', 'IZZIIIIIIIII', 'ZZIIIIIIIIII', 'IIIIIIIIIIIX', 'IIIIIIIIIIXI', 'IIIIIIIIIXII', 'IIIIIIIIXIII', 'IIIIIIIXIIII', 'IIIIIIXIIIII', 'IIIIIXIIIIII', 'IIIIXIIIIIII', 'IIIXIIIIIIII', 'IIXIIIIIIIII', 'IXIIIIIIIIII', 'XIIIIIIIIIII'],\n",
" coeffs=[ 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n",
" 1.+0.j, 1.+0.j, 1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j,\n",
" -1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j, -1.+0.j])\n"
]
}
],
"source": [
"from qiskit.quantum_info import SparsePauliOp\n",
"\n",
"# define the number of qubits\n",
"n = 12\n",
"\n",
"# define the single Pauli terms as (\"Paulis\", [indices], coefficient)\n",
"interactions = [\n",
" (\"ZZ\", [i, i + 1], 1) for i in range(n - 1)\n",
"] # we assume spins on a 1D line\n",
"field = [(\"X\", [i], -1) for i in range(n)]\n",
"\n",
"# build the operator\n",
"hamiltonian = SparsePauliOp.from_sparse_list(\n",
" interactions + field, num_qubits=n\n",
")\n",
"print(hamiltonian)"
]
},
{
"cell_type": "markdown",
"id": "a8624db2-9658-48e4-ae16-d2dcabe1d822",
"metadata": {},
"source": [
"If we would like to measure the energy the observable is the Hamiltonian itself. Alternatively, we could be\n",
"interested in measuring system properties like the average magnetization by counting the number of spins\n",
"aligned in the $Z$-direction with the observable\n",
"\n",
"$$\n",
"O = \\frac{1}{n} \\sum_{i=1} Z_i\n",
"$$\n",
"\n",
"For observables that are not given in terms of Pauli operators but in a matrix form, we first have to reformulate them\n",
"in the Pauli basis in order to evaluate them on a quantum computer.\n",
"We are always able to find such a representation as the Pauli matrices form a basis for the Hermitian $2^n \\times 2^n$ matrices.\n",
"We expand the observable $O$ as\n",
"\n",
"$$\n",
"O = \\sum_{P \\in \\{I, X, Y, Z\\}^{\\otimes n}} \\mathrm{Tr}(O P) P,\n",
"$$\n",
"\n",
"where the sum runs over all possible $n$-qubit Pauli terms and $\\mathrm{Tr}(\\cdot)$ is the trace of a matrix, which acts as inner product.\n",
"You can implement this decomposition from a matrix to Pauli terms using the `SparsePauliOp.from_operator` method, like so:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "82856434-b283-4bb2-a9b0-300c2c9388af",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SparsePauliOp(['IZ', 'XI', 'YY'],\n",
" coeffs=[-1. +0.j, 0.5+0.j, 1. -0.j])\n"
]
}
],
"source": [
"import numpy as np\n",
"from qiskit.quantum_info import SparsePauliOp\n",
"\n",
"matrix = np.array(\n",
" [[-1, 0, 0.5, -1], [0, 1, 1, 0.5], [0.5, 1, -1, 0], [-1, 0.5, 0, 1]]\n",
")\n",
"\n",
"observable = SparsePauliOp.from_operator(matrix)\n",
"print(observable)"
]
},
{
"cell_type": "markdown",
"id": "444aa9c6-1b6f-4834-a96d-1fcba9e0a3af",
"metadata": {},
"source": [
"This means the matrix can be written as Pauli terms as $O = -Z_1 + 0.5 X_2 + Y_2 Y_1$.\n",
"\n",
"<Admonition type=\"note\">\n",
" Remember the tensor product order maps to qubits as $q_n \\otimes q_{n-1} \\otimes \\cdots \\otimes q_1$.\n",
"</Admonition>\n",
"\n",
"<Admonition type=\"note\">\n",
" If the observable is Hermitian (meaning $O^\\dagger = O$), the Pauli coefficients are real numbers.\n",
" We can, however, also decompose any other complex matrix in terms of Paulis, if we allow for complex-valued coefficients.\n",
"</Admonition>\n",
"\n",
"\n",
"## Measure in Pauli bases\n",
"\n",
"A measurement projects the qubit state to the computational basis $\\{|0\\rangle, |1\\rangle\\}$. This implies that you can only measure observables that are diagonal in this basis, such as Paulis consisting only of $I$ and $Z$ terms.\n",
"Measuring arbitrary Pauli terms therefore requires a change of basis to diagonalize them. To do this, perform the following transformations,\n",
"\n",
"$$\n",
"\\begin{aligned}\n",
" X &\\rightarrow Z = H X H \\\\\n",
" Y &\\rightarrow Z = H S^\\dagger Y S H,\n",
"\\end{aligned}\n",
"$$\n",
"\n",
"where $H$ is the Hadamard gate and $S = \\sqrt{Z}$ is sometimes referred to as the phase gate.\n",
"If you are using an [Estimator](../api/qiskit/qiskit.primitives.Estimator) to compute expectation values, the basis transformations are automatically performed.\n",
"\n",
"Below is an example demonstrating how to prepare a quantum circuit and manually measure the qubit 0 in the X basis,\n",
"qubit 1 in the Y basis and qubit 2 in the Z basis.\n",
"We apply the transformations shown in the previous equation and obtain the following circuit:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ce4b1984-ebe0-44f6-a78c-d67b9e9bb361",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg width=\"567.94pt\" height=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 567.94 218.36\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style></defs><path d=\"m0 218.36h567.94v-218.36h-567.94z\" fill=\"#ffffff\"/><path d=\"m87.926 44.283h468.18\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m87.926 90.637h468.18\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m87.926 136.99h468.18\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m97.197 187.98 4.6354-9.2708\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m87.926 181.84h468.18\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m87.926 184.85h468.18\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m431.41 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#0add68b66ae)\" fill=\"#778899\"/><path d=\"m477.76 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#0add68b66ae)\" fill=\"#778899\"/><path d=\"m524.12 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#0add68b66ae)\" fill=\"#778899\"/><path d=\"m83.29 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#0add68b66ae)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m159.31 90.637v-46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m205.66 136.99v-46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m438.94 44.283v128.52\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m435.93 44.283v128.52\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m485.3 90.637v82.162\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m482.28 90.637v82.162\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m531.65 136.99v35.808\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m528.64 136.99v35.808\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m242.98 67.46h18.078v-46.354h-18.078z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m242.98 113.81h18.078v-46.354h-18.078z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m242.98 160.17h18.078v-46.354h-18.078z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m382.04 67.46h18.078v-46.354h-18.078z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m382.04 113.81h18.078v-46.354h-18.078z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m382.04 160.17h18.078v-46.354h-18.078z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m97.892 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m159.31 48.803c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958 0-1.1986-0.4762-2.3482-1.3237-3.1958-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237-1.1986 0-2.3482 0.4762-3.1958 1.3237s-1.3237 1.9972-1.3237 3.1958c0 1.1986 0.4762 2.3482 1.3237 3.1958s1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#0add68b66ae)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m159.31 101.18c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#0add68b66ae)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m205.66 95.157c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958s-0.4762-2.3482-1.3237-3.1958c-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237-1.1986 0-2.3482 0.4762-3.1958 1.3237-0.84753 0.84753-1.3237 1.9972-1.3237 3.1958s0.4762 2.3482 1.3237 3.1958c0.84753 0.84753 1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#0add68b66ae)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m205.66 147.54c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#0add68b66ae)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m283.31 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m283.31 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m329.66 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m422.37 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m447.98 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m437.43 48.803 10.546-10.546\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m468.72 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m494.33 95.157c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887c-2.7958 0-5.4799 1.1118-7.4568 3.0887s-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m483.79 95.157 10.546-10.546\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m515.08 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#0add68b66ae)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m540.69 141.51c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m530.14 141.51 10.546-10.546\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m159.31 96.663v-12.052\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m153.28 90.637h12.052\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m205.66 143.02v-12.052\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m199.64 136.99h12.052\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(60.617 48.734) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-Oblique-71\" transform=\"scale(.015625)\" d=\"m2669 525q-231-303-546-460-314-156-695-156-531 0-833 358-301 358-301 986 0 506 186 978t533 847q225 244 517 375t614 131q387 0 637-153t363-462l100 525h578l-934-4813h-579l360 1844zm-1778 813q0-463 193-705 194-242 560-242 544 0 928 520t384 1264q0 450-199 689-198 239-569 239-272 0-504-127-231-126-403-370-181-256-286-600-104-343-104-668z\"/><path id=\"DejaVuSans-30\" transform=\"scale(.015625)\" d=\"m2034 4250q-487 0-733-480-245-479-245-1442 0-959 245-1439 246-480 733-480 491 0 736 480 246 480 246 1439 0 963-246 1442-245 480-736 480zm0 500q785 0 1199-621 414-620 414-1801 0-1178-414-1799-414-620-1199-620-784 0-1198 620-414 621-414 1799 0 1181 414 1801 414 621 1198 621z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(60.617 95.088) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(60.617 141.44) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-32\" transform=\"scale(.015625)\" d=\"m1228 531h2203v-531h-2962v531q359 372 979 998 621 627 780 809 303 340 423 576 121 236 121 464 0 372-261 606-261 235-680 235-297 0-627-103-329-103-704-313v638q381 153 712 231 332 78 607 78 725 0 1156-363 431-362 431-968 0-288-108-546-107-257-392-607-78-91-497-524-418-433-1181-1211z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(92.561 176.55) scale(.104 -.104)\"><defs><path id=\"DejaVuSans-33\" transform=\"scale(.015625)\" d=\"m2597 2516q453-97 707-404 255-306 255-756 0-690-475-1069-475-378-1350-378-293 0-604 58t-642 174v609q262-153 574-231 313-78 654-78 593 0 904 234t311 681q0 413-289 645-289 233-804 233h-544v519h569q465 0 712 186t247 536q0 359-255 551-254 193-729 193-260 0-557-57-297-56-653-174v562q360 100 674 150t592 50q719 0 1137-327 419-326 419-882 0-388-222-655t-631-370z\"/></defs><use xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(34.404 187.83) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-6d\" transform=\"scale(.015625)\" d=\"m3328 2828q216 388 516 572t706 184q547 0 844-383 297-382 297-1088v-2113h-578v2094q0 503-179 746-178 244-543 244-447 0-707-297-259-296-259-809v-1978h-578v2094q0 506-178 748t-550 242q-441 0-701-298-259-298-259-808v-1978h-578v3500h578v-544q197 322 472 475t653 153q382 0 649-194 267-193 395-562z\"/><path id=\"DejaVuSans-65\" transform=\"scale(.015625)\" d=\"m3597 1894v-281h-2644q38-594 358-905t892-311q331 0 642 81t618 244v-544q-310-131-635-200t-659-69q-838 0-1327 487-489 488-489 1320 0 859 464 1363 464 505 1252 505 706 0 1117-455 411-454 411-1235zm-575 169q-6 471-264 752-258 282-683 282-481 0-770-272t-333-766l2050 4z\"/><path id=\"DejaVuSans-61\" transform=\"scale(.015625)\" d=\"m2194 1759q-697 0-966-159t-269-544q0-306 202-486 202-179 548-179 479 0 768 339t289 901v128h-572zm1147 238v-1997h-575v531q-197-318-491-470t-719-152q-537 0-855 302-317 302-317 808 0 590 395 890 396 300 1180 300h807v57q0 397-261 614t-733 217q-300 0-585-72-284-72-546-216v532q315 122 612 182 297 61 578 61 760 0 1135-394 375-393 375-1193z\"/><path id=\"DejaVuSans-73\" transform=\"scale(.015625)\" d=\"m2834 3397v-544q-243 125-506 187-262 63-544 63-428 0-642-131t-214-394q0-200 153-314t616-217l197-44q612-131 870-370t258-667q0-488-386-773-386-284-1061-284-281 0-586 55t-642 164v594q319-166 628-249 309-82 613-82 406 0 624 139 219 139 219 392 0 234-158 359-157 125-692 241l-200 47q-534 112-772 345-237 233-237 639 0 494 350 762 350 269 994 269 318 0 599-47 282-46 519-140z\"/></defs><use xlink:href=\"#DejaVuSans-6d\"/><use x=\"97.412109\" xlink:href=\"#DejaVuSans-65\"/><use x=\"158.935547\" xlink:href=\"#DejaVuSans-61\"/><use x=\"220.214844\" xlink:href=\"#DejaVuSans-73\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(106.6 55.53) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-2e\" transform=\"scale(.015625)\" d=\"m684 794h660v-794h-660v794z\"/><path id=\"DejaVuSans-38\" transform=\"scale(.015625)\" d=\"m2034 2216q-450 0-708-241-257-241-257-662 0-422 257-663 258-241 708-241t709 242q260 243 260 662 0 421-258 662-257 241-711 241zm-631 268q-406 100-633 378-226 279-226 679 0 559 398 884 399 325 1092 325 697 0 1094-325t397-884q0-400-227-679-226-278-629-378 456-106 710-416 255-309 255-755 0-679-414-1042-414-362-1186-362-771 0-1186 362-414 363-414 1042 0 446 256 755 257 310 713 416zm-231 997q0-362 226-565 227-203 636-203 407 0 636 203 230 203 230 565 0 363-230 566-229 203-636 203-409 0-636-203-226-203-226-566z\"/></defs><use xlink:href=\"#DejaVuSans-30\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/><use x=\"95.410156\" xlink:href=\"#DejaVuSans-38\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(105.42 43.351) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-52\" transform=\"scale(.015625)\" d=\"m2841 2188q203-69 395-294t386-619l641-1275h-679l-596 1197q-232 469-449 622t-592 153h-688v-1972h-631v4666h1425q800 0 1194-335 394-334 394-1009 0-441-205-732-205-290-595-402zm-1582 1959v-1656h794q456 0 689 211t233 620-233 617-689 208h-794z\"/><path id=\"DejaVuSans-59\" transform=\"scale(.015625)\" d=\"m-13 4666h679l1293-1919 1285 1919h678l-1650-2444v-2222h-634v2222l-1651 2444z\"/></defs><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><path d=\"m252.02 21.106v46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><path d=\"m252.02 67.46v46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><path d=\"m252.02 113.81v46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(293.48 47.87) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-48\" transform=\"scale(.015625)\" d=\"m628 4666h631v-1913h2294v1913h631v-4666h-631v2222h-2294v-2222h-631v4666z\"/></defs><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(289.92 94.224) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-53\" transform=\"scale(.015625)\" d=\"m3425 4513v-616q-359 172-678 256-319 85-616 85-515 0-795-200t-280-569q0-310 186-468 186-157 705-254l381-78q706-135 1042-474t336-907q0-679-455-1029-454-350-1332-350-331 0-705 75-373 75-773 222v650q384-215 753-325 369-109 725-109 540 0 834 212 294 213 294 607 0 343-211 537t-692 291l-385 75q-706 140-1022 440-315 300-315 835 0 619 436 975t1201 356q329 0 669-60 341-59 697-177z\"/><path id=\"DejaVuSans-2020\" transform=\"scale(.015625)\" d=\"m1325 4666h550v-1313h1147v-478h-1147v-3491h-550v3491h-1147v478h1147v1313z\"/></defs><use transform=\"translate(0 .68438)\" xlink:href=\"#DejaVuSans-53\"/><use transform=\"translate(78.071 38.966) scale(.7)\" xlink:href=\"#DejaVuSans-2020\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(339.84 94.224) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><path d=\"m391.08 21.106v46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><path d=\"m391.08 67.46v46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><path d=\"m391.08 113.81v46.354\" clip-path=\"url(#0add68b66ae)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(449.02 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(495.38 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#0add68b66ae)\"><g transform=\"translate(541.73 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-32\"/></g></g><defs><clipPath id=\"0add68b66ae\"><rect x=\"7.2\" y=\"7.2\" width=\"553.54\" height=\"203.96\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 998.442x367.889 with 1 Axes>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from qiskit.circuit import QuantumCircuit\n",
"\n",
"# create a circuit, where we would like to measure\n",
"# q0 in the X basis, q1 in the Y basis and q2 in the Z basis\n",
"circuit = QuantumCircuit(3)\n",
"circuit.ry(0.8, 0)\n",
"circuit.cx(0, 1)\n",
"circuit.cx(1, 2)\n",
"circuit.barrier()\n",
"\n",
"# diagonalize X with the Hadamard gate\n",
"circuit.h(0)\n",
"\n",
"# diagonalize Y with Hadamard as S^\\dagger\n",
"circuit.sdg(1)\n",
"circuit.h(1)\n",
"\n",
"# the Z basis is the default, no action required here\n",
"\n",
"# measure all qubits\n",
"circuit.measure_all()\n",
"circuit.draw(\"mpl\")"
]
},
{
"cell_type": "markdown",
"id": "6e52d357-b225-4c50-abba-7d78211e2fa5",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
" - See an example of circuit decomposition in the [Variational quantum eigensolver](https://learning.quantum.ibm.com/tutorial/variational-quantum-eigensolver) tutorial.\n",
" - Read the [SparsePauliOp API](/api/qiskit/qiskit.quantum_info.SparsePauliOp#sparsepauliop) reference.\n",
"</Admonition>"
]
}
],
"metadata": {
"description": "Measure circuits in different Pauli bases, which is required to measure observables that are not diagonal in the computational basis.",
"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": "Specify observables in the Pauli basis"
},
"nbformat": 4,
"nbformat_minor": 5
}