ScQ-Cloud / pyquafu

PyQuafu is designed for users to construct, compile, and execute quantum circuits on quantum devices on Quafu using Python.
Apache License 2.0
65 stars 23 forks source link

issue with cif #175

Open Zhaoyilunnn opened 2 months ago

Zhaoyilunnn commented 2 months ago
from quafu import User
import numpy as np
user = User("xx")
user.save_apitoken()

available_backends = user.get_available_backends()

import numpy as np
from quafu import QuantumCircuit

qc1 = QuantumCircuit(3)

#Initialization

#Input
qc1.x(0)

#Preparation of other qubits. Note that here we use an equivalent circuit of Bell gate, which is implemented in PRA 97, 032320 (2018).

qc1.z(1)
qc1.h(2)
qc1.cz(1, 2)
qc1.h(1)
qc1.cz(1, 2)
qc1.h(2)

#Main process

qc1.cx(0, 1)
qc1.h(0)

qc1.measure([0, 1],  [0, 1])

qc1.x(2).cif([1], 1)
qc1.z(2).cif([0], 1)

# Obtaining the final result

qc1.measure([2],  [2])

qc2 = QuantumCircuit(3)

#Initialization

#Input
qc2.x(0)

#Preparation of other qubits. Note that here we use an equivalent circuit of Bell gate, which is implemented in PRA 97, 032320 (2018).

qc2.z(1)
qc2.h(2)
qc2.cz(1, 2)
qc2.h(1)
qc2.cz(1, 2)
qc2.h(2)

#Main process

qc2.cx(0, 1)
qc2.h(0)

qc2.cx(1, 2)
qc2.cz(0, 2)

measures0 = [0, 1]
cbits0 = [0, 1]
qc2.measure(measures0,  cbits=cbits0)

# Obtaining the final result
measures2 = [2]
cbits2 = [2]
qc2.measure(measures2,  cbits=cbits2)

from quafu import Task
from quafu import simulate
simu_res1 = simulate(qc1)
simu_res1.plot_probabilities()

simu_res2 = simulate(qc2)
simu_res2.plot_probabilities()

Expect results of qc1 to have four cases 001, 011, 101, 111. But only output 111

Zhaoyilunnn commented 2 months ago

Actually the simulator returns the correct counts. The reason of this bug is due to the dependency of state vector to calculate the ideal probability distribution. However, for dynamic circuit current implementation does not support this as it simulate same circuit for multiple times, thus the probability only reflects the last execution resutls.

See: https://github.com/ScQ-Cloud/pyquafu/blob/edd008300e0248dec0541a8a76bbbe1df1333f44/src/qfvm/qfvm.cpp#L108-L137