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

circuit_EPR = qc2(2)

# 1. put a Hadamard gate in the qubit 0 position
circuit_EPR.h(0)

# 2. put a CNOT gate, control qubit 0, target qubit 1
circuit_EPR.cx(0, 1)

# 3. draw the circuit
circuit_EPR.show_circ()

circuit_EPR.to_matrix()

circuit_EPR.show_matrix()

# Create a state vector of |00>
state00 = sv2.from_label('00')

# Show the state of this sv2 object 
state00.show_state()

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

# Show the state of this sv2 object
state_x0.show_state()

state00.to_matrix()

state_x0.to_matrix()

state00.show_matrix()

state_x0.show_matrix()

# The state of |00> after the EPR circuit
state_EPR = state00.evolve(circuit_EPR)

# Show the state of this sv2 object
state_EPR.show_state()

# Create a GHZ state
state_GHZ = sv2.from_label("000", "111")

# Measure the first qubit of the GHZ state, and display the result
state_GHZ.show_measure(0)

# After measuring the first and second qubits of the EPR pair, the state vector corresponding to each measurement result is stored in a list
list_after_measure_bit01 = state_GHZ.measure([0,1])

# Show the state vector after measured and the measurement result = 00
list_after_measure_bit01[0b00].show_state()

# This is None, because the GHZ measurement of the 0,1 qubit, the result of 00 is not possible
list_after_measure_bit01[0b01] is None

# show_state() preset to show state in z-basis
state_x0.show_state() # 1/√2(|0>+|1>)

# Show state with X basis
state_x0.show_state(basis='x') # |+>

# Automatically determine the basis (show the basis with the smallest entropy)
state_x0.show_state(basis="") # |+>

# measure() / show_measure() preset to measure in Z basis
state_GHZ.show_measure(0)  # |0>: |00>, |1>: |11>

# Measure in X basis
state_GHZ.show_measure(0, basis='x')  # |+>: 1/√2(|00>+|11>), |->: 1/√2(|00>-|11>)