Skip to content

Commit

Permalink
fix qiskit bug when using a name of an existing gate for our gate
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 31, 2023
1 parent 55f9131 commit d4de3fc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions qlasskit/compiler/internalcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def compile(self, name, args: Args, returns: Arg, exprs: BoolExpList) -> QCircui
is_temp = sym.name[0:2] == "__"
symp_exp = self._symplify_exp(exp)

# X = Y: perform a "copy"
# if isinstance(exp, Symbol):
# iret = qc.get_free_ancilla()
# qc.cx(qc[exp], iret)
# else:
iret = self.compile_expr(qc, symp_exp)

self.expqmap[sym] = iret
Expand Down
5 changes: 5 additions & 0 deletions qlasskit/qcircuit/exporter_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def export(self, _selfqc, mode: Literal["circuit", "gate"]): # noqa: C901
if mode == "gate":
qc.remove_final_measurements()
gate = qc.to_gate()

gate.name = _selfqc.name

if hasattr(QuantumCircuit, _selfqc.name):
gate.name += '_'

return gate
elif mode == "circuit":
return qc
Expand Down
15 changes: 15 additions & 0 deletions test/test_qlassf_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ def test_list(self):
self.assertEqual(qf.expressions[0][0], _ret)
self.assertEqual(qf.expressions[0][1], And(a_0, a_1))
compute_and_compare_results(self, qf)

def test_list_item_swap(self):
f = "def swapf(a: Qlist[Qint2, 2]) -> Qlist[Qint2, 2]:\n\treturn [a[1], a[0]]"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_list_item_swap_bool(self):
f = "def swapf(a: Qlist[bool, 2]) -> Qlist[bool, 2]:\n\treturn [a[1], a[0]]"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_list_item_sum(self):
f = "def swapf(a: Qlist[Qint2, 2]) -> Qint2:\n\treturn a[0] + a[1]"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)
5 changes: 5 additions & 0 deletions test/test_qlassf_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def test_tuple_arg(self):
self.assertEqual(qf.expressions[0][1], And(a_0, a_1))
compute_and_compare_results(self, qf)

def test_tuple_item_swap(self):
f = "def swapf(a: Tuple[Qint2, Qint2]) -> Tuple[Qint2, Qint2]:\n\treturn (a[1], a[0])"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_tuple_ite(self):
f = "def test(b: bool, a: Tuple[bool, bool]) -> Tuple[bool,bool]:\n\treturn (a[1],a[0]) if b else a"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
Expand Down
9 changes: 5 additions & 4 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def compute_result_of_qcircuit(cls, qf, truth_line):
circ = qf.circuit()
gate = qf.gate()
qc = QuantumCircuit(gate.num_qubits)

# Prepare inputs
[qc.initialize(1 if truth_line[i] else 0, i) for i in range(qf.input_size)]

qc.append(gate, list(range(qf.num_qubits)))

# print(qc.decompose().draw("text"))
Expand All @@ -102,7 +102,7 @@ def compute_result_of_qcircuit(cls, qf, truth_line):
qf.input_size
+ len(qf.expressions)
+ sum([gateinputcount(compiler.optimizer(e[1])) for e in qf.expressions])
)
)

cls.assertLessEqual(qf.gate().num_qubits, max_qubits)

Expand Down Expand Up @@ -175,9 +175,10 @@ def compute_and_compare_results(cls, qf, test_original_f=True):
elif COMPILATION_ENABLED:
qc_truth = truth_table

# circ_qi = qf.circuit().export("circuit", "qiskit")
circ_qi = qf.circuit().export("circuit", "qiskit")
# print(qf.expressions)
# print(circ_qi.draw("text"))
# print(circ_qi.qasm())

for truth_line in truth_table:
# Extract str of truthtable and result
Expand Down

0 comments on commit d4de3fc

Please sign in to comment.