from qiskit_extension.quantum_circuit2 import QuantumCircuit2 as qc2
from qiskit_extension.state_vector2 import StateVector2 as sv2

circuit_EPR = qc2(2)

# 1. 在qubit 0 的位置放入一個Hadamard gate
circuit_EPR.h(0)

# 2. 放入一個CNOT gate, 控制qubit為0, 目標qubit為1
circuit_EPR.cx(0, 1)

# 3. 繪製電路圖
circuit_EPR.show_circ()

circuit_EPR.to_matrix()

circuit_EPR.show_matrix()

# 建立一個|00>的state vector
state00 = sv2.from_label('00')

# 顯示出此sv2物件的狀態 
state00.show_state()

# 建立state vector: 2( √2|0> + |+> - |-> )
state_x0 = sv2.from_label((2**0.5,"0"),"+",(-1,"-"))

# 顯示出此sv2物件的狀態
state_x0.show_state()

state00.to_matrix()

state_x0.to_matrix()

state00.show_matrix()

state_x0.show_matrix()

# 將|00>經過EPR電路後的狀態
state_EPR = state00.evolve(circuit_EPR)

# 顯示出此sv2物件的狀態
state_EPR.show_state()

# 建立一個GHZ state
state_GHZ = sv2.from_label("000","111")

# 量測GHZ state的第一個qubit, 並顯示出結果
state_GHZ.show_measure(0)

# 量測EPR pair的第一,二顆qubit後，將各種量測結果對應到的state vector存入一個list中
list_after_measure_bit01 = state_GHZ.measure([0,1])

# 顯示出量測到00的後state vector
list_after_measure_bit01[0b00].show_state()

# 此為None, 因為GHZ量測第0,1顆qubit，不可能出現01的結果
list_after_measure_bit01[0b01] is None

# show_state() 預設以Z基底顯示狀態
state_x0.show_state()  # 1/√2(|0>+|1>)

# 以X基底顯示狀態
state_x0.show_state(basis='x')  # |+>

# 自動判斷基底(以最小entropy的基底顯示)
state_x0.show_state(basis="")  # |+>

# measure() / show_measure() 預設以Z基底量測
state_GHZ.show_measure(0)  # |0>: |00>, |1>: |11>

# 以X基底量測
state_GHZ.show_measure(0, basis='x')  # |+>: 1/√2(|00>+|11>), |->: 1/√2(|00>-|11>)