Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix qfasm for barrier #114

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quafu/circuits/quantum_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def gates(self, gates: list):
# TODO(qtzhuang): add_gates is just a temporary call function to add gate from gate_list
def add_gates(self, gates: list):
for gate in gates:
self.add_gate(gate)
self.add_ins(gate)

def add_gate(self, gate: QuantumGate):
"""
Expand Down
18 changes: 14 additions & 4 deletions quafu/qfasm/qfasm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,30 @@ def handle_gateins(self, gateins: GateInstruction):
gate_list.append(gate_classes[gateins.name](*oneargs))

# if op is barrier or reset or measure
elif gateins.name in ["reset", "barrier"]:
nametoclass = {"reset": Reset, "barrier": Barrier}
elif gateins.name == "reset":
for qarg in gateins.qargs:
symnode = self.global_symtab[qarg.name]
if isinstance(qarg, Id):
qlist = []
for i in range(symnode.num):
qlist.append(symnode.start + i)
gate_list.append(nametoclass[gateins.name](qlist))
gate_list.append(Reset(qlist))
elif isinstance(qarg, IndexedId):
gate_list.append(
nametoclass[gateins.name]([symnode.start + qarg.num])
Reset([symnode.start + qarg.num])
)

elif gateins.name == "barrier":
qlist = []
for qarg in gateins.qargs:
symnode = self.global_symtab[qarg.name]
if isinstance(qarg, Id):
for i in range(symnode.num):
qlist.append(symnode.start + i)
elif isinstance(qarg, IndexedId):
qlist.append(symnode.start + qarg.num)
gate_list.append(Barrier(qlist))

# we have check the num of cbit and qbit
elif gateins.name == "measure":
bitmap = {}
Expand Down
2 changes: 1 addition & 1 deletion tests/quafu/algorithms/angle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_build(self):
qc = QuantumCircuit(num_qubits)
feature = np.array([[6,-12.5,11.15,7],[8,9.5,-11,-5],[5,0.5,8,-7]])
for i in range(4):
qc.add_gate(qeg.HGate(pos=i))
qc.add_ins(qeg.HGate(pos=i))
for i in range(len(feature)):
qc.add_gates(AngleEmbedding(features=feature[i], num_qubits=num_qubits,rotation='Y'))
qc.draw_circuit(width=num_qubits)
30 changes: 30 additions & 0 deletions tests/quafu/qasm/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,40 @@ def test_qubit_barrier_in_definition(self):
qc = QuantumCircuit(2)
qc.barrier([0])
qc.barrier([1])
qc.barrier([0, 1])
self.compare_cir(cir, qc)

def test_barrier_single(self):
qasm = """
qreg q[2];
barrier q[0];
barrier q[1];
barrier q;
"""
cir = qasm_to_quafu(qasm)
qc = QuantumCircuit(2)
qc.barrier([0])
qc.barrier([1])
qc.barrier([0, 1])
self.compare_cir(cir, qc)

def test_barrier_mul(self):
qasm = """
qreg q[2];
qreg q2[2];
barrier q[0], q[1];
barrier q[1], q2[0];
barrier q[0], q2;
barrier q, q2[0];
"""
cir = qasm_to_quafu(qasm)
qc = QuantumCircuit(4)
qc.barrier([0,1])
qc.barrier([1,2])
qc.barrier([0,2,3])
qc.barrier([0,1,2])
self.compare_cir(cir, qc)

def test_double_call_gate(self):
qasm = """
gate test(x, y) a {
Expand Down