{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# Measuring subsets of qubits\n", "\n", "\n", "In this notebook, we explore the possibility to measure a subset of qubits of a given output state.\n", "\n", "## First example (Bell state)\n", "\n", "\n", "Let us consider the Bell state $$|\\psi\\rangle=\\frac{|00\\rangle+|11\\rangle}{\\sqrt{2}}$$\n", "\n", "Its vector of amplitudes is $(1/\\sqrt{2},0,0,1/\\sqrt{2})$.\n", "\n", "Let us measure its first qubit. Then we should obtain \n", "\n", "- the value 0 with probability p=1/2, in which case the system is in state $|00\\rangle$\n", "\n", "- the value 1 with probability p=1/2, in which case the system is in state $|11\\rangle$\n", "\n", "The same holds for the second qubit since the state is invariant under a swap of its qubits.\n", "\n", "In the cell below, we create a circuit whose output is this Bell state, and output the nonzero amplitudes: " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "State |00>, amplitude (0.7071067811865475+0j), probability 0.4999999999999999\n", "State |11>, amplitude (0.7071067811865475+0j), probability 0.4999999999999999\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAJkAAABMCAMAAABJceUUAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAGxQTFRF////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////fMh50wAAACJ0Uk5TAEBEUNqI37sid2Yz3cxVqhHume/I5s3x7Y/WtlvYXOLprzri4xUAAAABYktHRACIBR1IAAAACXBIWXMAAABIAAAASABGyWs+AAACiElEQVRo3u2Y23LbIBCG6SZEQoA4JGmbHtKW93/IcpBkYiMjVDPydPgvrJEXlg9YQCxCTU1NTU13qk9wTQ8Hkj3ia3o6kKz7B2sja2T3SdZjTBAuJfOLcXCVraAKGWUUUb7RaVRsNCIgmqHOmEnlHCs3ZkBLyAYz+qcwKesNpJX96Q0gKmDUcjuZNqGw6lLW8/7jrsM552dS2vffurXBMurtZIz7BzU4ZT0D48aKF6FRHyW6Q9L4t81k/UREDOTJtPHauMxmMueYYQQeyvQnE0mcjI+LdTDC/8Ojzqyeqs+B7GnNnhxMBa4VmMkggk58TbxEwxD+MdFAvax9hLwGss9r9uSgjQKIW14TWWZ5njASYXbb2bTRCS7MbNygGW8LWSrMrqwAVb4CQv9dK2Vrc97NtElZL9H0F166a6Cwm9l56SiIPlN0aXvezViXsl6rWSCsu3DwrURi0v8UZv2H2Lk1WYmC/97uFcr2BjAzDPdn1iPJ9lgbWSP7H8j234Rrk+3PHhx5r7quRtbIGtl9qpE1skZ2jwL89W2UR1NcalQcfyNCld44S7KheyQFp342JWb5e2OkomzoHnX+4u8bAJa7asc9OmVDUT4bukMk3JlD10nBCCzZ0J6IGgMnGY3IUDdsrrlkQ3uJa5ANUxJn8j1ubmPJhlpVIdPDBzKUy4NFZFM29JKM4Ky+54s8v033quldFWdDL8koZPUjX+Tne3jOmdBfxdnQWrNp98p4NqXaXnXKhtYim0N+egyiqHYIszpkiI0RmSw7BUI2FDBXuKjeNoGSJzJcdAou2dBKwu7YDGSYyYoNlYswIh0ZcHFfYDZchOK//yg+Hg2SEryDPJqhqampqcnpL9eQIojcrGWGAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE5LTExLTEzVDExOjI1OjM0KzAxOjAw9FnXQwAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxOS0xMS0xM1QxMToyNTozNCswMTowMIUEb/8AAAAUdEVYdHBkZjpWZXJzaW9uAFBERi0xLjUgBVwLOQAAAABJRU5ErkJggg==\n", "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qat.lang.AQASM import Program, H, CNOT, X\n", "qprog = Program()\n", "nbqbits = 2\n", "qbits = qprog.qalloc(nbqbits)\n", "qprog.apply(H, qbits[0]) \n", "qprog.apply(CNOT, qbits[0], qbits[1])\n", "\n", "circuit = qprog.to_circ()\n", "\n", "from qat.qpus import PyLinalg\n", "result = PyLinalg().submit(circuit.to_job())\n", "for sample in result:\n", " print(\"State %s, amplitude %s, probability %s\"%(sample.state, sample.amplitude, sample.probability))\n", "\n", "%qatdisplay circuit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now call the ``to_job`` method of the task with two arguments: \n", "\n", "- the first (``qubits``) specifies the indices of the qubits we want to measure, \n", "- the second (``nbshots``) the number of repetitions of the measurement." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Qubit 0 has value 0 (the probability of getting this result is 0.46 +/- None)\n", "Qubit 0 has value 1 (the probability of getting this result is 0.54 +/- None)\n", "\n", "Qubit 1 has value 0 (the probability of getting this result is 0.52 +/- None)\n", "Qubit 1 has value 1 (the probability of getting this result is 0.48 +/- None)\n" ] } ], "source": [ "# measuring qubit #0 50 times\n", "result = PyLinalg().submit(circuit.to_job(qubits=[0], nbshots=50))\n", "for res in result:\n", " print(\"Qubit 0 has value %s (the probability of getting this result is %s +/- %s)\"%(int(res.state[0]),\n", " res.probability, res.err))\n", "print()\n", "\n", "# measuring qubit #1 50 times\n", "result = PyLinalg().submit(circuit.to_job(qubits=[1], nbshots=50))\n", "for res in result:\n", " print(\"Qubit 1 has value %s (the probability of getting this result is %s +/- %s)\"%(int(res.state[0]), \n", " res.probability, res.err))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 2\n", "\n", "In this section, we are interested in a quantum state of the form $$|\\psi\\rangle=\\frac{|00\\rangle}{\\sqrt{2}}+\\frac{|10\\rangle+|11\\rangle}{2}$$\n", "\n", "Its amplitudes are $(1/\\sqrt{2},1/2,0, 1/2)$.\n", "\n", "Let us measure its first qubit. Then we should obtain \n", "\n", "- the value 0 with probability p=0.5\n", "\n", "- the value 1 with probability p=0.5\n", "\n", "Let us measure its second qubit. Then we should obtain \n", "\n", "- the value 0 with probability p=0.5+0.25=0.75,\n", "\n", "- the value 1 with probability p=0.25.\n", "\n", "Let us construct the circuit that prepares this state:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.lang.AQASM import Program, H, CNOT\n", "\n", "qprog = Program()\n", "nbqbits = 2\n", "qbits = qprog.qalloc(nbqbits)\n", "\n", "qprog.apply(H, qbits[0])\n", "qprog.apply(H.ctrl(), qbits[0], qbits[1])\n", "circuit = qprog.to_circ(submatrices_only=False)\n", "%qatdisplay circuit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us check we obtain the correct amplitudes:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = PyLinalg().submit(circuit.to_job())\n", "for sample in result:\n", " print(\"State %s, amplitude %s, probability %s\"%(sample.state, sample.amplitude, sample.probability))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us execute partial measurements." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# measuring qubit #0 200 times\n", "result = PyLinalg().submit(circuit.to_job(qubits=[0], nbshots=200))\n", "for res in result:\n", " print(\"Qubit 0 has value %s (the probability of getting this result is %s)\"%(int(res.state[0]), res.probability))\n", "print()\n", "\n", "# measuring qubit #1 200 times\n", "result = PyLinalg().submit(circuit.to_job(qubits=[1], nbshots=200))\n", "for res in result:\n", " print(\"Qubit 1 has value %s (the probability of getting this result is %s)\"%(int(res.state[0]), res.probability))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 3: measuring several qubits\n", "\n", "\n", "In this last section, we are interested in a quantum state of the form $$|\\psi\\rangle=\\frac{|000\\rangle}{\\sqrt{2}}+\\frac{|100\\rangle+|111\\rangle}{2}$$\n", "\n", "Its amplitudes are $(1/\\sqrt{2},0,0,0, 1/2, 0, 0, 1/2)$.\n", "\n", "Let us measure its first and second qubit. Then we should obtain \n", "\n", "- the value 0,0 with probability $p=0.5$\n", "- the value 0,1 with probability $p=0$\n", "- the value 1,0 with probability $p=0.25$\n", "- the value 1,1 with probability $p=0.25$\n", "\n", "Let us measure its second and third qubit. Then we should obtain \n", "\n", "- the value 0,0 with probability $p=0.75$\n", "- the value 0,1 with probability $p=0$\n", "- the value 1,0 with probability $p=0$\n", "- the value 1,1 with probability $p=0.25$\n", "\n", "Let us first build the circuit that prepares this state, and check the amplitudes are correct:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.lang.AQASM import Program, H, CNOT\n", "\n", "qprog = Program()\n", "nbqbits = 3\n", "qbits = qprog.qalloc(nbqbits)\n", "\n", "qprog.apply(H, qbits[0])\n", "qprog.apply(H.ctrl(), qbits[0], qbits[1])\n", "qprog.apply(CNOT, qbits[1], qbits[2])\n", "circuit = qprog.to_circ(submatrices_only=False)\n", "\n", "result = PyLinalg().submit(circuit.to_job())\n", "\n", "for sample in result:\n", " print(\"State %s, amplitude %s, probability %s\"%(sample.state, sample.amplitude, sample.probability))\n", "\n", "%qatdisplay circuit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us now perform measurements on q0, q1 on the one hand, and q1, q2 on the other hand:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# measuring qubit #0 and #1 200 times\n", "result = PyLinalg().submit(circuit.to_job(qubits=[0, 1], nbshots=200))\n", "for res in result:\n", " print(\"Qubits 0,1 have value %s,%s (the probability of getting this result is %s +/- %s)\"%(int(res.state[0]),\n", " int(res.state[1]),\n", " res.probability,\n", " res.err))\n", "print()\n", "\n", "# measuring qubit #1 and #2 200 times\n", "result = PyLinalg().submit(circuit.to_job(qubits=[1, 2], nbshots=200))\n", "for res in result:\n", " print(\"Qubits 1,2 have value %s,%s (the probability of getting this result is %s +/- %s)\"%(int(res.state[0]),\n", " int(res.state[1]),\n", " res.probability,\n", " res.err))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "authors": [ "Simon Martiel" ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }