|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import cirq\n", |
| 10 | + "from cirq.contrib.svg import SVGCircuit\n", |
| 11 | + "import numpy as np\n", |
| 12 | + "from cirq_web import BlochSphere\n", |
| 13 | + "\n", |
| 14 | + "qc = cirq.Circuit()\n", |
| 15 | + "\n", |
| 16 | + "m = cirq.NamedQubit(\"q0 (message)\")\n", |
| 17 | + "A = cirq.NamedQubit(\"q1 (Alice)\")\n", |
| 18 | + "B = cirq.NamedQubit(\"q2 (Bob)\")\n", |
| 19 | + "\n", |
| 20 | + "message_gate = cirq.X ** 0.25\n", |
| 21 | + "\n", |
| 22 | + "qc.append(message_gate(m))\n", |
| 23 | + "\n", |
| 24 | + "message_state_vector = cirq.Circuit(message_gate.on(cirq.NamedQubit(\"m\"))).final_state_vector()\n", |
| 25 | + "sphere=BlochSphere(state_vector=message_state_vector)\n", |
| 26 | + "display(sphere)\n", |
| 27 | + "\n", |
| 28 | + "qc.append(cirq.H(A))\n", |
| 29 | + "qc.append(cirq.CX(A,B))\n", |
| 30 | + "\n", |
| 31 | + "qc.append(cirq.CX(m,A))\n", |
| 32 | + "qc.append(cirq.H(m))\n", |
| 33 | + "\n", |
| 34 | + "qc.append(cirq.measure(A,m))\n", |
| 35 | + "\n", |
| 36 | + "qc.append([cirq.CNOT(A,B), cirq.CZ(m,B)])\n", |
| 37 | + "\n", |
| 38 | + "s = cirq.Simulator()\n", |
| 39 | + "\n", |
| 40 | + "result = s.simulate(qc)\n", |
| 41 | + "\n", |
| 42 | + "message_bloch_vector = cirq.bloch_vector_from_state_vector(message_state_vector,index=0)\n", |
| 43 | + "print(\"Bloch vector of message qubit:\")\n", |
| 44 | + "print(np.round(message_bloch_vector, 3))\n", |
| 45 | + "print()\n", |
| 46 | + "print(qc)\n", |
| 47 | + "print()\n", |
| 48 | + "bobs_bloch_vector = cirq.bloch_vector_from_state_vector(result.final_state_vector, index=2)\n", |
| 49 | + "print(\"Bloch vector of Bob's qubit:\")\n", |
| 50 | + "print(np.round(bobs_bloch_vector, 3))\n", |
| 51 | + "\n", |
| 52 | + "state_vector = cirq.to_valid_state_vector(result.final_state_vector)\n", |
| 53 | + "state_vector = cirq.sub_state_vector(state_vector,[2])\n", |
| 54 | + "sphere=BlochSphere(state_vector=state_vector)\n", |
| 55 | + "display(sphere)\n", |
| 56 | + "\n", |
| 57 | + "SVGCircuit(qc)\n" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [] |
| 66 | + } |
| 67 | + ], |
| 68 | + "metadata": { |
| 69 | + "colab": { |
| 70 | + "name": "start.ipynb", |
| 71 | + "toc_visible": true |
| 72 | + }, |
| 73 | + "kernelspec": { |
| 74 | + "display_name": "Python 3 (ipykernel)", |
| 75 | + "language": "python", |
| 76 | + "name": "python3" |
| 77 | + }, |
| 78 | + "language_info": { |
| 79 | + "codemirror_mode": { |
| 80 | + "name": "ipython", |
| 81 | + "version": 3 |
| 82 | + }, |
| 83 | + "file_extension": ".py", |
| 84 | + "mimetype": "text/x-python", |
| 85 | + "name": "python", |
| 86 | + "nbconvert_exporter": "python", |
| 87 | + "pygments_lexer": "ipython3", |
| 88 | + "version": "3.10.12" |
| 89 | + } |
| 90 | + }, |
| 91 | + "nbformat": 4, |
| 92 | + "nbformat_minor": 4 |
| 93 | +} |
0 commit comments