diff --git a/qlasskit/compiler/internalcompiler.py b/qlasskit/compiler/internalcompiler.py index f4216f0d..4a59252e 100644 --- a/qlasskit/compiler/internalcompiler.py +++ b/qlasskit/compiler/internalcompiler.py @@ -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 diff --git a/qlasskit/qcircuit/exporter_qiskit.py b/qlasskit/qcircuit/exporter_qiskit.py index b0411ea3..6515d7f3 100644 --- a/qlasskit/qcircuit/exporter_qiskit.py +++ b/qlasskit/qcircuit/exporter_qiskit.py @@ -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 diff --git a/test/test_qlassf_list.py b/test/test_qlassf_list.py index edc56bc7..85d0a41a 100644 --- a/test/test_qlassf_list.py +++ b/test/test_qlassf_list.py @@ -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) diff --git a/test/test_qlassf_tuple.py b/test/test_qlassf_tuple.py index a6a8a24d..755cb17a 100644 --- a/test/test_qlassf_tuple.py +++ b/test/test_qlassf_tuple.py @@ -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) diff --git a/test/utils.py b/test/utils.py index 945a5052..f566961c 100644 --- a/test/utils.py +++ b/test/utils.py @@ -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")) @@ -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) @@ -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