{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Bell's Inequality\n", "\n", "This tutorial shows how to run a version of Bell's inequality experiment in Braket on a local simulator and a QPU. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References \n", "\n", "[1] Bell, J. S. On the Einstein Podolsky Rosen Paradox. Physics Physique Fizika 1, no. 3 (November 1, 1964): 195–200. https://doi.org/10.1103/PhysicsPhysiqueFizika.1.195. \n", "\n", "[2] Greenberger, Daniel M., Michael A. Horne, Abner Shimony, and Anton Zeilinger (1990). Bell’s Theorem without Inequalities. American Journal of Physics 58, no. 12: 1131–43. https://doi.org/10.1119/1.16243. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Run on a local simulator" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from braket.devices import LocalSimulator\n", "from braket.tracking import Tracker\n", "\n", "from braket.experimental.algorithms.bells_inequality import (\n", " create_bell_inequality_circuits,\n", " get_bell_inequality_results,\n", " run_bell_inequality,\n", ")\n", "\n", "tracker = Tracker().start() # to keep track of Braket costs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bell's Inequality experiment consists of three circuits acting on two qubits each. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Circuit AB\n", " T : |0|1|2| 3 |Result Types|\n", " \n", "q0 : -X-H-C----------Probability--\n", " | | \n", "q1 : -X---X-Rx(1.05)-Probability--\n", "\n", "T : |0|1|2| 3 |Result Types|\n", "\n", "Circuit AC\n", " T : |0|1|2| 3 |Result Types|\n", " \n", "q0 : -X-H-C----------Probability--\n", " | | \n", "q1 : -X---X-Rx(2.09)-Probability--\n", "\n", "T : |0|1|2| 3 |Result Types|\n", "\n", "Circuit BC\n", " T : |0|1|2| 3 |Result Types|\n", " \n", "q0 : -X-H-C-Rx(1.05)-Probability--\n", " | | \n", "q1 : -X---X-Rx(2.09)-Probability--\n", "\n", "T : |0|1|2| 3 |Result Types|\n" ] } ], "source": [ "circAB, circAC, circBC = create_bell_inequality_circuits(0, 1)\n", "print(\"\\nCircuit AB\\n\", circAB)\n", "print(\"\\nCircuit AC\\n\", circAC)\n", "print(\"\\nCircuit BC\\n\", circBC)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The three circuits are grouped together in the `run_bell_inequality` function below. To run on a local noise-free simulator, we can call this function. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[LocalQuantumTask('id':5b2fd071-176c-4566-820d-e559f7400534), LocalQuantumTask('id':819ede49-79f9-482d-bf16-6a4845cb887e), LocalQuantumTask('id':ea4ce7ff-8d39-4d1d-9339-8a366cace40c)]\n" ] } ], "source": [ "local_simulator = LocalSimulator()\n", "local_tasks = run_bell_inequality([circAB, circAC, circBC], local_simulator, shots=1000)\n", "print(local_tasks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To gether the results of Bell's inequality test, we call the `get_bell_inequality_results` of the tasks from above. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P(a,b) = -0.47,P(a,c) = 0.526,P(b,c) = -0.49\n", "Bell's' inequality: 1.486 ≤ 1\n", "Bell's inequality is violated!\n" ] } ], "source": [ "results, pAB, pAC, pBC = get_bell_inequality_results(local_tasks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Run on a QPU\n", "\n", "To run Bell's inequality on a QPU, we replace the LocalSimulator with an AwsDevice. \n", "The cost to run this experiment is \\\\$0.3 per task and \\\\$0.00035 per shot on the Oxford Quantum Circuits Lucy device. \n", "Since we have three circuits of 1000 shots each, that totals \\$1.95 USD." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# # Uncomment to run on a QPU\n", "# from braket.aws import AwsDevice\n", "\n", "# oqc_lucy = AwsDevice(\"arn:aws:braket:eu-west-2::device/qpu/oqc/Lucy\")\n", "# oqc_tasks = run_bell_inequality([circAB, circAC, circBC], oqc_lucy, shots=1000)\n", "# results, pAB, pAC, pBC = get_bell_inequality_results(oqc_tasks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that Bell's inequality is violated, so the device is demonstrating quantum behavior." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Task Summary\n", "{'arn:aws:braket:eu-west-2::device/qpu/oqc/Lucy': {'shots': 3000, 'tasks': {'COMPLETED': 3}}} \n", "\n", "Estimated cost to run this example: 1.95 USD\n" ] } ], "source": [ "print(\"Task Summary\")\n", "print(f\"{tracker.quantum_tasks_statistics()} \\n\")\n", "print(f\"Estimated cost to run this example: {tracker.qpu_tasks_cost() + tracker.simulator_tasks_cost():.2f} USD\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: Charges shown are estimates based on your Amazon Braket simulator and quantum processing unit (QPU) task usage. Estimated charges shown may differ from your actual charges. Estimated charges do not factor in any discounts or credits, and you may experience additional charges based on your use of other services such as Amazon Elastic Compute Cloud (Amazon EC2)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.5" }, "vscode": { "interpreter": { "hash": "5904cb9a2089448a2e1aeb5d493d227c9de33e591d7c07e4016fb81e71061a5d" } } }, "nbformat": 4, "nbformat_minor": 4 }