{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# GHZ state\n",
"$\\newcommand{\\bra}[1]{\\left\\langle{#1}\\right|}$\n",
"$\\newcommand{\\ket}[1]{\\left|{#1}\\right\\rangle}$\n",
"\n",
"The purpose of this notebook is to simulate the GHZ experiment\n",
"described in the IBM Quantum Experience tutorial in the section\n",
"entitled \n",
"\n",
">Multiple Qubits, Gates, and Entangled States/GHZ states\n",
" \n",
"If you understand our \"Bell_and_CHSH_inequalities\" notebook,\n",
"this notebook uses very similar math.\n",
"\n",
"It uses the following results whose proofs use techniques already covered \n",
"in our \"Bell_and_CHSH_inequalities\" notebook\n",
"\n",
"$\\bra{ b_X} = \\bra{ b_Z} H$\n",
"\n",
"$\\bra{ b_Y} = \\bra{ b_Z} H S^\\dagger$\n",
"\n",
"for $b=0, 1$.\n",
"\n",
"$\\bra{\\psi} \\sigma_A(0) \\sigma_B(1) \\sigma_C(2)\\ket{\\psi} = \\sum_{b_0 + b_1 + b_2 = 0, 2} Prob(b_0, b_1, b_2) - \\sum_{b_0 + b_1 + b_2 = 1, 3} Prob(b_0, b_1, b_2)$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First change your working directory to the qubiter directory in your computer, and add its path to the path environment variable."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/rrtucci/PycharmProjects/qubiter/qubiter/jupyter_notebooks\n",
"/home/rrtucci/PycharmProjects/qubiter\n"
]
}
],
"source": [
"import os\n",
"import sys\n",
"print(os.getcwd())\n",
"os.chdir('../../')\n",
"print(os.getcwd())\n",
"sys.path.insert(0,os.getcwd())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loaded OneQubitGate, WITHOUT autograd.numpy\n"
]
}
],
"source": [
"from qubiter.SEO_writer import *\n",
"from qubiter.SEO_simulator import *\n",
"from qubiter.StateVec import *\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def write_ghz_plus(file_prefix, ghz_only=True, meas=None):\n",
" num_qbits = 3\n",
" z_axis = 3\n",
" emb = CktEmbedder(num_qbits, num_qbits)\n",
" print('-------------------', file_prefix)\n",
" wr = SEO_writer(file_prefix, emb)\n",
" wr.write_one_qbit_gate(0, OneQubitGate.had2)\n",
" wr.write_one_qbit_gate(1, OneQubitGate.had2)\n",
" wr.write_one_qbit_gate(2, OneQubitGate.sigx)\n",
"\n",
" control_pos = 0\n",
" target_pos = 2\n",
" trols = Controls.new_single_trol(num_qbits, control_pos, kind=True)\n",
" wr.write_controlled_one_qbit_gate(\n",
" target_pos, trols, OneQubitGate.sigx)\n",
"\n",
" control_pos = 1\n",
" target_pos = 2\n",
" trols = Controls.new_single_trol(num_qbits, control_pos, kind=True)\n",
" wr.write_controlled_one_qbit_gate(\n",
" target_pos, trols, OneQubitGate.sigx)\n",
"\n",
" wr.write_one_qbit_gate(0, OneQubitGate.had2)\n",
" wr.write_one_qbit_gate(1, OneQubitGate.had2)\n",
" wr.write_one_qbit_gate(2, OneQubitGate.had2)\n",
"\n",
" if not ghz_only:\n",
" for pos in range(3):\n",
" m = meas[pos]\n",
" if m == 1:\n",
" wr.write_one_qbit_gate(pos, OneQubitGate.had2)\n",
" elif m == 2:\n",
" wr.write_one_qbit_gate(pos,\n",
" OneQubitGate.rot_ax, [np.pi/4, z_axis]) # S^\\dagger(pos)\n",
" wr.write_one_qbit_gate(pos, OneQubitGate.had2)\n",
" else:\n",
" assert False\n",
" wr.close_files()\n",
" wr.print_pic_file(jup=True)\n",
" init_st_vec = StateVec.get_standard_basis_st_vec([0, 0, 0])\n",
" sim = SEO_simulator(file_prefix, num_qbits, init_st_vec)\n",
" StateVec.describe_st_vec_dict(sim.cur_st_vec_dict, print_st_vec=True, do_pp=True,\n",
" omit_zero_amps=True, show_pp_probs=True)\n",
" fin_st_vec = sim.cur_st_vec_dict[\"pure\"]\n",
" print('Prob(bit0=i, bit1=j, bit2=k) for i,j,k,=0,1:')\n",
" prob_arr = np.abs(fin_st_vec.arr)**2\n",
" print(prob_arr)\n",
" mean = prob_arr[0, 0, 0] \\\n",
" + prob_arr[0, 1, 1] \\\n",
" + prob_arr[1, 0, 1] \\\n",
" + prob_arr[1, 1, 0] \\\n",
" - prob_arr[1, 1, 1] \\\n",
" - prob_arr[0, 0, 1] \\\n",
" - prob_arr[1, 0, 0] \\\n",
" - prob_arr[0, 1, 0]\n",
" print('mean=', mean)\n",
" return mean\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------- ghz_zzz_meas\n"
]
},
{
"data": {
"text/html": [
"
1 | | | H |
2 | | H | | 3 | X | | | 4 | X---+---@ | 5 | X---@ | | 6 | | | H | 7 | | H | | 8 | H | | |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"*********branch= pure\n",
"state vector:\n",
"ZL convention (Zero bit Last in state tuple)\n",
"(000)ZL ( 0.707107 + 0.000000j)\t prob=0.500000\n",
"(111)ZL (-0.707107 + 0.000000j)\t prob=0.500000\n",
"total probability of state vector (=one if no measurements)= 1.000000\n",
"dictionary with key=qubit, value=(Prob(0), Prob(1))\n",
"{0: (0.5, 0.5), 1: (0.5, 0.5), 2: (0.5, 0.5)}\n",
"Prob(bit0=i, bit1=j, bit2=k) for i,j,k,=0,1:\n",
"[[[5.00000000e-01 0.00000000e+00]\n",
" [1.93646401e-37 0.00000000e+00]]\n",
"\n",
" [[0.00000000e+00 1.93646401e-37]\n",
" [0.00000000e+00 5.00000000e-01]]]\n",
"mean= -1.936464009786347e-37\n"
]
}
],
"source": [
"# sigz(0)sigz(1)sigz(2) measurement\n",
"file_prefix = 'ghz_zzz_meas'\n",
"mean_zzz = write_ghz_plus(file_prefix, ghz_only=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------- ghz_yyx_meas\n"
]
},
{
"data": {
"text/html": [
"1 | | | H |
2 | | H | | 3 | X | | | 4 | X---+---@ | 5 | X---@ | | 6 | | | H | 7 | | H | | 8 | H | | | 9 | | | Rz | 10 | | | H | 11 | | Rz | | 12 | | H | | 13 | H | | |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"*********branch= pure\n",
"state vector:\n",
"ZL convention (Zero bit Last in state tuple)\n",
"(000)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(110)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(101)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(011)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"total probability of state vector (=one if no measurements)= 1.000000\n",
"dictionary with key=qubit, value=(Prob(0), Prob(1))\n",
"{0: (0.5, 0.5), 1: (0.5, 0.5), 2: (0.5, 0.5)}\n",
"Prob(bit0=i, bit1=j, bit2=k) for i,j,k,=0,1:\n",
"[[[2.50000000e-01 6.98526388e-33]\n",
" [6.88162536e-33 2.50000000e-01]]\n",
"\n",
" [[6.98526388e-33 2.50000000e-01]\n",
" [2.50000000e-01 6.88162536e-33]]]\n",
"mean= 0.9999999999999987\n"
]
}
],
"source": [
"# sigy(0)sigy(1)sigx(2) measurement\n",
"file_prefix = 'ghz_yyx_meas'\n",
"mean_yyx = write_ghz_plus(file_prefix, ghz_only=False, meas=[2, 2, 1])\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------- ghz_yxy_meas\n"
]
},
{
"data": {
"text/html": [
"1 | | | H |
2 | | H | | 3 | X | | | 4 | X---+---@ | 5 | X---@ | | 6 | | | H | 7 | | H | | 8 | H | | | 9 | | | Rz | 10 | | | H | 11 | | H | | 12 | Rz | | | 13 | H | | |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"*********branch= pure\n",
"state vector:\n",
"ZL convention (Zero bit Last in state tuple)\n",
"(000)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(110)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(101)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(011)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"total probability of state vector (=one if no measurements)= 1.000000\n",
"dictionary with key=qubit, value=(Prob(0), Prob(1))\n",
"{0: (0.5, 0.5), 1: (0.5, 0.5), 2: (0.5, 0.5)}\n",
"Prob(bit0=i, bit1=j, bit2=k) for i,j,k,=0,1:\n",
"[[[2.50000000e-01 7.26573057e-33]\n",
" [7.26573057e-33 2.50000000e-01]]\n",
"\n",
" [[7.26573057e-33 2.50000000e-01]\n",
" [2.50000000e-01 7.26573057e-33]]]\n",
"mean= 0.9999999999999987\n"
]
}
],
"source": [
"# sigy(0)sigx(1)sigy(2) measurement\n",
"file_prefix = 'ghz_yxy_meas'\n",
"mean_yxy = write_ghz_plus(file_prefix, ghz_only=False, meas=[2, 1, 2])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------- ghz_xyy_meas\n"
]
},
{
"data": {
"text/html": [
"1 | | | H |
2 | | H | | 3 | X | | | 4 | X---+---@ | 5 | X---@ | | 6 | | | H | 7 | | H | | 8 | H | | | 9 | | | H | 10 | | Rz | | 11 | | H | | 12 | Rz | | | 13 | H | | |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"*********branch= pure\n",
"state vector:\n",
"ZL convention (Zero bit Last in state tuple)\n",
"(000)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(110)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(101)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"(011)ZL ( 0.000000 + 0.500000j)\t prob=0.250000\n",
"total probability of state vector (=one if no measurements)= 1.000000\n",
"dictionary with key=qubit, value=(Prob(0), Prob(1))\n",
"{0: (0.5, 0.5), 1: (0.5, 0.5), 2: (0.5, 0.5)}\n",
"Prob(bit0=i, bit1=j, bit2=k) for i,j,k,=0,1:\n",
"[[[2.50000000e-01 7.26573057e-33]\n",
" [7.26573057e-33 2.50000000e-01]]\n",
"\n",
" [[7.26573057e-33 2.50000000e-01]\n",
" [2.50000000e-01 7.26573057e-33]]]\n",
"mean= 0.9999999999999987\n"
]
}
],
"source": [
"# sigx(0)sigy(1)sigy(2) measurement\n",
"file_prefix = 'ghz_xyy_meas'\n",
"mean_xyy = write_ghz_plus(file_prefix, ghz_only=False, meas=[1, 2, 2])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------- ghz_xxx_meas\n"
]
},
{
"data": {
"text/html": [
"1 | | | H |
2 | | H | | 3 | X | | | 4 | X---+---@ | 5 | X---@ | | 6 | | | H | 7 | | H | | 8 | H | | | 9 | | | H | 10 | | H | | 11 | H | | |
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"*********branch= pure\n",
"state vector:\n",
"ZL convention (Zero bit Last in state tuple)\n",
"(100)ZL ( 0.500000 + 0.000000j)\t prob=0.250000\n",
"(010)ZL ( 0.500000 + 0.000000j)\t prob=0.250000\n",
"(001)ZL ( 0.500000 + 0.000000j)\t prob=0.250000\n",
"(111)ZL ( 0.500000 + 0.000000j)\t prob=0.250000\n",
"total probability of state vector (=one if no measurements)= 1.000000\n",
"dictionary with key=qubit, value=(Prob(0), Prob(1))\n",
"{0: (0.5, 0.5), 1: (0.5, 0.5), 2: (0.5, 0.5)}\n",
"Prob(bit0=i, bit1=j, bit2=k) for i,j,k,=0,1:\n",
"[[[0. 0.25]\n",
" [0.25 0. ]]\n",
"\n",
" [[0.25 0. ]\n",
" [0. 0.25]]]\n",
"mean= -0.9999999999999987\n"
]
}
],
"source": [
"# sigx(0)sigx(1)sigx(2) measurement\n",
"file_prefix = 'ghz_xxx_meas'\n",
"mean_xxx = write_ghz_plus(file_prefix, ghz_only=False, meas=[1, 1, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let\n",
"\n",
"$mean\\_abc = \\bra{\\psi} \\sigma_A(0) \\sigma_B(1)\\sigma_C(2)\\ket{\\psi}$\n",
"\n",
"where\n",
"\n",
"$\\ket{\\psi} = \\frac{1}{\\sqrt{2}}(\\ket{000} + \\ket{111})$\n",
"\n",
"We expect\n",
"\n",
"$mean\\_yyx = mean\\_yxy = mean\\_xyy = 1$\n",
"\n",
"and \n",
"\n",
"$mean\\_xxx=-1$.\n",
"\n",
"This could never happen classically."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-----------------------\n",
"mean_yyx = 0.9999999999999987\n",
"mean_yxy = 0.9999999999999987\n",
"mean_xyy = 0.9999999999999987\n",
"mean_xxx = -0.9999999999999987\n"
]
}
],
"source": [
"print('-----------------------')\n",
"print('mean_yyx =', mean_yyx)\n",
"print('mean_yxy =', mean_yxy)\n",
"print('mean_xyy =', mean_xyy)\n",
"print('mean_xxx =', mean_xxx)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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.10.9"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "30px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false,
"widenNotebook": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}