qiskitで量子もつれ その4

# 概要
量子もつれが、分からないので、copilotに聞いてみた。
ベルの不等式の破れを実験したい。

# 参考にしたページ

https://github.com/lvillasen/Testing-Bell-Inequality-on-a-Quantum-Computer


# サンプルコード

```

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.primitives import StatevectorSampler as Sampler
#from qiskit import QuantumCircuit, Aer
from qiskit import transpile
import qiskit.quantum_info as qi
import numpy as np

qc = QuantumCircuit(2, 2)
# Ponemos 2 qubits y 2 bits
qc.h(0)
qc.cx(0, 1)
qc.x(1)
qc.z(1)
qc.barrier()
stv = qi.Statevector.from_instruction(qc)
#qc.draw("mpl")
#stv.draw('latex', prefix="|\psi\rangle =")
#from qiskit_aer import AerSimulator
#from qiskit import IBMQ, transpile
backend = AerSimulator()
#.from_backend(backend)
shots = 1000
N_corr = 0
N_anticorr = 0
qc3 = QuantumCircuit(2, 2)
qc3.measure(0, 0)
qc3.measure(1, 1)
print("						 Experiment A1B1")
qc11 = qc
qc11 = qc.compose(qc3)
print(qc11)
tqc11 = transpile(qc11, backend, optimization_level = 3)
job11 = backend.run(tqc11, shots = shots)
print("Results :", job11.result().get_counts())
if job11.result().get_counts().get('11') is not None:
	N_corr += job11.result().get_counts()['11']
if job11.result().get_counts().get('00') is not None:
	N_corr += job11.result().get_counts()['00']
N_anticorr += job11.result().get_counts()['01'] + job11.result().get_counts()['10']

print("\n						 Experiment A1B2")
qc12 = QuantumCircuit(2, 2)
qc12.rz(0, 1)
qc12.ry(-2 * np.pi / 3, 1)
qc12.barrier()
qc12 = qc.compose(qc12)
qc12 = qc12.compose(qc3)
print(qc12)
tqc12 = transpile(qc12, backend, optimization_level = 3)
job12 = backend.run(tqc12, shots = shots)
print("Results :", job12.result().get_counts())
N_corr += job12.result().get_counts()['11'] + job12.result().get_counts()['00']
N_anticorr += job12.result().get_counts()['01'] + job12.result().get_counts()['10']

print("\n						 Experiment A1B3")
qc13 = QuantumCircuit(2, 2)
qc13.rz(-np.pi, 1)
qc13.ry(-2 * np.pi / 3, 1)
qc13.barrier()
qc13 = qc.compose(qc13)
qc13 = qc13.compose(qc3)
print(qc13)
tqc13 = transpile(qc13, backend, optimization_level = 3)
job13 = backend.run(tqc13, shots = shots)
print("Results :", job13.result().get_counts())
N_corr += job13.result().get_counts()['11'] + job13.result().get_counts()['00']
N_anticorr += job13.result().get_counts()['01'] + job13.result().get_counts()['10']

print("\n						 Experiment A2B1")
qc21 = QuantumCircuit(2, 2)
qc21.rz(0, 0)
qc21.ry(-2 * np.pi / 3, 0)
qc21.barrier()
qc21 = qc.compose(qc21)
qc21 = qc21.compose(qc3)
print(qc21)
tqc21 = transpile(qc21, backend, optimization_level = 3)
job21 = backend.run(tqc21, shots = shots)
print("Results :", job21.result().get_counts())
N_corr += job21.result().get_counts()['11'] + job21.result().get_counts()['00']
N_anticorr += job21.result().get_counts()['01'] + job21.result().get_counts()['10']

print("\n						Experiment A2B2")
qc22 = QuantumCircuit(2, 2)
qc22.rz(0, 0)
qc22.ry(-2 * np.pi / 3, 0)
qc22.rz(0, 1)
qc22.ry(-2 * np.pi / 3, 1)
qc22.barrier()
qc22 = qc.compose(qc22)
qc22 = qc22.compose(qc3)
print(qc22)
tqc22 = transpile(qc22, backend, optimization_level = 3)
job22 = backend.run(tqc22, shots = shots)
print("Results :", job22.result().get_counts())
if job22.result().get_counts().get('11') is not None:
	N_corr += job22.result().get_counts()['11']
if job22.result().get_counts().get('00') is not None:
	N_corr += job22.result().get_counts()['00']
N_anticorr += job22.result().get_counts()['01'] + job22.result().get_counts()['10']

print("\n						Experiment A2B3")
qc23 = QuantumCircuit(2, 2)
qc23.rz(0, 0)
qc23.ry(-2 * np.pi / 3, 0)
qc23.rz(-np.pi, 1)
qc23.ry(-2 * np.pi / 3, 1)
qc23.barrier()
qc23 = qc.compose(qc23)
qc23 = qc23.compose(qc3)
print(qc23)
tqc23 = transpile(qc23, backend, optimization_level = 3)
job23 = backend.run(tqc23, shots = shots)
print("Results :", job23.result().get_counts())
N_corr += job23.result().get_counts()['11'] + job23.result().get_counts()['00']
N_anticorr += job23.result().get_counts()['01'] + job23.result().get_counts()['10']

print("\n						Experiment A3B1")
qc31 = QuantumCircuit(2, 2)
qc31.rz(-np.pi, 0)
qc31.ry(-2 * np.pi / 3, 0)
qc31.barrier()
qc31 = qc.compose(qc31)
qc31 = qc31.compose(qc3)
print(qc31)
tqc31 = transpile(qc31, backend, optimization_level = 3)
job31 = backend.run(tqc31, shots = shots)
print("Results :", job31.result().get_counts())
N_corr += job31.result().get_counts()['11'] + job31.result().get_counts()['00']
N_anticorr += job31.result().get_counts()['01'] + job31.result().get_counts()['10']

print("\n						Experiment A3B2")
qc32 = QuantumCircuit(2, 2)
qc32.rz(-np.pi, 0)
qc32.ry(-2 * np.pi / 3, 0)
qc32.rz(0,1)
qc32.ry(-2 * np.pi / 3, 1)
qc32.barrier()
qc32 = qc.compose(qc32)
qc32 = qc32.compose(qc3)
print(qc32)
tqc32 = transpile(qc32, backend, optimization_level = 3)
job32 = backend.run(tqc32, shots = shots)
print("Results :", job32.result().get_counts(qc32))
N_corr += job32.result().get_counts()['11'] + job32.result().get_counts()['00']
N_anticorr += job32.result().get_counts()['01'] + job32.result().get_counts()['10']

print("\n						Experiment A3B3")
qc33 = QuantumCircuit(2, 2)
qc33.rz(-np.pi, 0)
qc33.ry(-2 * np.pi / 3, 0)
qc33.rz(-np.pi, 1)
qc33.ry(-2 * np.pi / 3, 1)
qc33.barrier()
qc33 = qc.compose(qc33)
qc33 = qc33.compose(qc3)
print(qc33)
tqc33 = transpile(qc33, backend, optimization_level = 3)
job33 = backend.run(tqc33, shots = shots)
print("Results :", job33.result().get_counts(qc33))
if job33.result().get_counts().get('11') is not None:
	N_corr += job33.result().get_counts()['11']
if job33.result().get_counts().get('00') is not None:
	N_corr += job33.result().get_counts()['00']
N_anticorr += job33.result().get_counts()['01'] + job33.result().get_counts()['10']
print("\nOut of a total of", 9 * shots, "measurements, we obtained")
print("Total correlated measurements:", N_corr)
print("Total anti-correlated measurements:", N_anticorr," corresponding to ", np.round(100 * N_anticorr / (9 * shots), 2), "%")





```

# 実行結果

```
$ python3 test6.py
                                                 Experiment A1B1
     ┌───┐                ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░──╫─┤M├
          └───┘└───┘└───┘ ░  ║ └╥┘
c: 2/════════════════════════╩══╩═
                             0  1
Results : {'01': 477, '10': 523}

                                                 Experiment A1B2
     ┌───┐                ░                        ░ ┌─┐
q_0: ┤ H ├──■─────────────░────────────────────────░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ ┌───────┐┌───────────┐ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─┤ Rz(0) ├┤ Ry(-2π/3) ├─░──╫─┤M├
          └───┘└───┘└───┘ ░ └───────┘└───────────┘ ░  ║ └╥┘
c: 2/═════════════════════════════════════════════════╩══╩═
                                                      0  1
Results : {'01': 136, '11': 383, '10': 128, '00': 353}

                                                 Experiment A1B3
     ┌───┐                ░                         ░ ┌─┐
q_0: ┤ H ├──■─────────────░─────────────────────────░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ ┌────────┐┌───────────┐ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─┤ Rz(-π) ├┤ Ry(-2π/3) ├─░──╫─┤M├
          └───┘└───┘└───┘ ░ └────────┘└───────────┘ ░  ║ └╥┘
c: 2/══════════════════════════════════════════════════╩══╩═
                                                       0  1
Results : {'01': 130, '11': 365, '00': 394, '10': 111}

                                                 Experiment A2B1
     ┌───┐                ░ ┌───────┐┌───────────┐ ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤ Rz(0) ├┤ Ry(-2π/3) ├─░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ └───────┘└───────────┘ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░────────────────────────░──╫─┤M├
          └───┘└───┘└───┘ ░                        ░  ║ └╥┘
c: 2/═════════════════════════════════════════════════╩══╩═
                                                      0  1
Results : {'01': 140, '11': 381, '10': 97, '00': 382}

                                                Experiment A2B2
     ┌───┐                ░ ┌───────┐┌───────────┐ ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤ Rz(0) ├┤ Ry(-2π/3) ├─░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ ├───────┤├───────────┤ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─┤ Rz(0) ├┤ Ry(-2π/3) ├─░──╫─┤M├
          └───┘└───┘└───┘ ░ └───────┘└───────────┘ ░  ║ └╥┘
c: 2/═════════════════════════════════════════════════╩══╩═
                                                      0  1
Results : {'10': 484, '01': 516}

                                                Experiment A2B3
     ┌───┐                ░ ┌───────┐ ┌───────────┐ ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤ Rz(0) ├─┤ Ry(-2π/3) ├─░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ ├───────┴┐├───────────┤ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─┤ Rz(-π) ├┤ Ry(-2π/3) ├─░──╫─┤M├
          └───┘└───┘└───┘ ░ └────────┘└───────────┘ ░  ║ └╥┘
c: 2/══════════════════════════════════════════════════╩══╩═
                                                       0  1
Results : {'01': 129, '11': 379, '00': 365, '10': 127}

                                                Experiment A3B1
     ┌───┐                ░ ┌────────┐┌───────────┐ ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤ Rz(-π) ├┤ Ry(-2π/3) ├─░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ └────────┘└───────────┘ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─────────────────────────░──╫─┤M├
          └───┘└───┘└───┘ ░                         ░  ║ └╥┘
c: 2/══════════════════════════════════════════════════╩══╩═
                                                       0  1
Results : {'01': 120, '11': 403, '10': 112, '00': 365}

                                                Experiment A3B2
     ┌───┐                ░ ┌────────┐┌───────────┐ ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤ Rz(-π) ├┤ Ry(-2π/3) ├─░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ ├───────┬┘├───────────┤ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─┤ Rz(0) ├─┤ Ry(-2π/3) ├─░──╫─┤M├
          └───┘└───┘└───┘ ░ └───────┘ └───────────┘ ░  ║ └╥┘
c: 2/══════════════════════════════════════════════════╩══╩═
                                                       0  1
Results : {'01': 151, '10': 123, '00': 366, '11': 360}

                                                Experiment A3B3
     ┌───┐                ░ ┌────────┐┌───────────┐ ░ ┌─┐
q_0: ┤ H ├──■─────────────░─┤ Rz(-π) ├┤ Ry(-2π/3) ├─░─┤M├───
     └───┘┌─┴─┐┌───┐┌───┐ ░ ├────────┤├───────────┤ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ X ├┤ Z ├─░─┤ Rz(-π) ├┤ Ry(-2π/3) ├─░──╫─┤M├
          └───┘└───┘└───┘ ░ └────────┘└───────────┘ ░  ║ └╥┘
c: 2/══════════════════════════════════════════════════╩══╩═
                                                       0  1
Results : {'10': 513, '01': 487}

Out of a total of 9000 measurements, we obtained
Total correlated measurements: 4496
Total anti-correlated measurements: 4504  corresponding to  50.04 %
```

以上。