# 导入相应的运算库
import numpy as np
from qiskit import BasicAer, execute
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.tools.visualization import circuit_drawer

desired_vector1 = [6/np.sqrt(200), 8/np.sqrt(200), 1/np.sqrt(2), 0]
desired_vector2 = [10/np.sqrt(101), -1/np.sqrt(101)]

q0 = QuantumRegister(1,'q0')
c0 = ClassicalRegister(1,'c0')

q1 = QuantumRegister(2,'q1')
c1 = ClassicalRegister(2,'c1')

q2 = QuantumRegister(1,'q2')
c2 = ClassicalRegister(1,'c2')

Circuit = QuantumCircuit()
Circuit.add_register(q0,c0)
Circuit.add_register(q1,c1)
Circuit.add_register(q2,c2)

Circuit.initialize(desired_vector1, [q1[0],q1[1]])
Circuit.initialize(desired_vector2, [q2[0]])

Circuit.h(q0[0])
Circuit.ccx(q0[0],q1[1],q2[0])
Circuit.ccx(q0[0],q2[0],q1[1])
Circuit.ccx(q0[0],q1[1],q2[0])
Circuit.h(q0[0])

Circuit.draw(output='mpl')

# 对寄存器q0进行测量
Circuit.measure(q0,c0)

# 测量次数设置为1024次
backend = BasicAer.get_backend('qasm_simulator')
job = execute(Circuit, backend,shots=1024)
job.result().get_counts(Circuit)

P_0 = 0.5-89/202*0.5
P_1 = 0.5+89/202*0.5
Times_0 = P_0*1024
Times_1 = P_1*1024
print('进行测量之后，辅助量子比特为0态的次数是',Times_0)
print('进行测量之后，辅助量子比特为1态的次数是',Times_1)