From 86000b64abfb9d83485289d9efd5c05bc4c7fe07 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Sun, 17 Nov 2024 19:39:03 +0700 Subject: [PATCH 01/11] modified the from_qiskit --- mitiq/interface/mitiq_qiskit/conversions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index 72fb4c94fb..2708f082a6 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -16,6 +16,7 @@ from cirq.contrib.qasm_import import circuit_from_qasm from cirq.contrib.qasm_import.exception import QasmException from qiskit import qasm2 +from qiskit import transpile from qiskit.transpiler import PassManager from qiskit.transpiler.layout import Layout from qiskit.transpiler.passes import SetLayout @@ -52,9 +53,7 @@ def _remove_qasm_barriers(qasm: QASMType) -> QASMType: return "".join(lines) -def _map_bit_index( - bit_index: int, new_register_sizes: List[int] -) -> Tuple[int, int]: +def _map_bit_index(bit_index: int, new_register_sizes: List[int]) -> Tuple[int, int]: """Returns the register index and (qu)bit index in this register for the mapped bit_index. @@ -255,6 +254,7 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit: # Try to decompose circuit before running # This is necessary for converting qiskit circuits with # custom packaged gates, e.g., QFT gates + circuit = circuit.decompose(gates_to_decompose=["qft"]) circuit = circuit.decompose() mitiq_circuit = from_qasm(qasm2.dumps(circuit)) return mitiq_circuit From e4a5d7282b7ba463bc01edfb9f046541f9811da6 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Sun, 17 Nov 2024 20:06:30 +0700 Subject: [PATCH 02/11] Added myself / the copyright holder to the AUTHORS file --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index d253a68a5b..b796b2cdd0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -59,3 +59,4 @@ Vladimir Kozhukalov Francesc Sabater Emiliano Godinez Tommy Nguyen +Duong H. D. Tran \ No newline at end of file From 566cea109bb5b6ee1997075a49fc53bf24a01a0a Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Sun, 17 Nov 2024 20:09:14 +0700 Subject: [PATCH 03/11] removed unused import --- mitiq/interface/mitiq_qiskit/conversions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index 2708f082a6..149c8b3fb9 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -16,7 +16,6 @@ from cirq.contrib.qasm_import import circuit_from_qasm from cirq.contrib.qasm_import.exception import QasmException from qiskit import qasm2 -from qiskit import transpile from qiskit.transpiler import PassManager from qiskit.transpiler.layout import Layout from qiskit.transpiler.passes import SetLayout From 138839cad5ab0796ef7e09a171664a2c0bef9b73 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Sun, 17 Nov 2024 20:22:11 +0700 Subject: [PATCH 04/11] undo styling --- mitiq/interface/mitiq_qiskit/conversions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index 149c8b3fb9..d8eb259259 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -52,7 +52,9 @@ def _remove_qasm_barriers(qasm: QASMType) -> QASMType: return "".join(lines) -def _map_bit_index(bit_index: int, new_register_sizes: List[int]) -> Tuple[int, int]: +def _map_bit_index( + bit_index: int, new_register_sizes: List[int] +) -> Tuple[int, int]: """Returns the register index and (qu)bit index in this register for the mapped bit_index. From 23ab614737fdd5e24c233f0f882dc377f72b5cd3 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Sun, 17 Nov 2024 22:32:01 +0700 Subject: [PATCH 05/11] modified from_qiskit function, added test for convert_to_mitiq --- mitiq/interface/mitiq_qiskit/conversions.py | 10 +++++----- .../mitiq_qiskit/tests/test_conversions_qiskit.py | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index d8eb259259..c18e2fb2ec 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -15,7 +15,7 @@ import qiskit from cirq.contrib.qasm_import import circuit_from_qasm from cirq.contrib.qasm_import.exception import QasmException -from qiskit import qasm2 +from qiskit import qasm2, transpile from qiskit.transpiler import PassManager from qiskit.transpiler.layout import Layout from qiskit.transpiler.passes import SetLayout @@ -52,9 +52,7 @@ def _remove_qasm_barriers(qasm: QASMType) -> QASMType: return "".join(lines) -def _map_bit_index( - bit_index: int, new_register_sizes: List[int] -) -> Tuple[int, int]: +def _map_bit_index(bit_index: int, new_register_sizes: List[int]) -> Tuple[int, int]: """Returns the register index and (qu)bit index in this register for the mapped bit_index. @@ -255,7 +253,9 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit: # Try to decompose circuit before running # This is necessary for converting qiskit circuits with # custom packaged gates, e.g., QFT gates - circuit = circuit.decompose(gates_to_decompose=["qft"]) + circuit = circuit.decompose( + gates_to_decompose=["u1", "u2", "u3", "cx", "rx", "ry"] + ) circuit = circuit.decompose() mitiq_circuit = from_qasm(qasm2.dumps(circuit)) return mitiq_circuit diff --git a/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py b/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py index 4ceb8d5d4d..2afbf24efe 100644 --- a/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py +++ b/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py @@ -476,3 +476,10 @@ def test_remove_identity_from_idle_with_multiple_registers(): input_multi, input_single = _multi_reg_circuits() assert circuit_multi_reg == input_multi assert circuit_single_reg == input_single + +def test_convert_to_mitiq_with_rx_and_rzz(): + """Tests that convert_to_mitiq works with RX and RZZ gates.""" + test_qc = qiskit.QuantumCircuit(2) + test_qc.rx(0.1, 0) + test_qc.rzz(0.1, 0, 1) + assert convert_to_mitiq(test_qc) \ No newline at end of file From 250525518af9b06eb8e7880914c3ca361ea306bb Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Mon, 18 Nov 2024 08:26:43 +0700 Subject: [PATCH 06/11] removed unused import --- mitiq/interface/mitiq_qiskit/conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index c18e2fb2ec..5287b3ee08 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -15,7 +15,7 @@ import qiskit from cirq.contrib.qasm_import import circuit_from_qasm from cirq.contrib.qasm_import.exception import QasmException -from qiskit import qasm2, transpile +from qiskit import qasm2 from qiskit.transpiler import PassManager from qiskit.transpiler.layout import Layout from qiskit.transpiler.passes import SetLayout From 91148ea23151b4eb2675419146d3607a4c2bc3d5 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Mon, 18 Nov 2024 15:09:46 +0700 Subject: [PATCH 07/11] make format --- mitiq/interface/mitiq_qiskit/conversions.py | 4 +++- mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index 5287b3ee08..a7d4c29b93 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -52,7 +52,9 @@ def _remove_qasm_barriers(qasm: QASMType) -> QASMType: return "".join(lines) -def _map_bit_index(bit_index: int, new_register_sizes: List[int]) -> Tuple[int, int]: +def _map_bit_index( + bit_index: int, new_register_sizes: List[int] +) -> Tuple[int, int]: """Returns the register index and (qu)bit index in this register for the mapped bit_index. diff --git a/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py b/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py index 2afbf24efe..52d146cf32 100644 --- a/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py +++ b/mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py @@ -477,9 +477,10 @@ def test_remove_identity_from_idle_with_multiple_registers(): assert circuit_multi_reg == input_multi assert circuit_single_reg == input_single + def test_convert_to_mitiq_with_rx_and_rzz(): """Tests that convert_to_mitiq works with RX and RZZ gates.""" test_qc = qiskit.QuantumCircuit(2) test_qc.rx(0.1, 0) test_qc.rzz(0.1, 0, 1) - assert convert_to_mitiq(test_qc) \ No newline at end of file + assert convert_to_mitiq(test_qc) From 54446a46518b890b2eb7dafca867f3e7b6822eb6 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Wed, 20 Nov 2024 11:22:24 +0700 Subject: [PATCH 08/11] changed gates_to_decompose --- mitiq/interface/mitiq_qiskit/conversions.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index a7d4c29b93..7dfe0fd85d 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -52,9 +52,7 @@ def _remove_qasm_barriers(qasm: QASMType) -> QASMType: return "".join(lines) -def _map_bit_index( - bit_index: int, new_register_sizes: List[int] -) -> Tuple[int, int]: +def _map_bit_index(bit_index: int, new_register_sizes: List[int]) -> Tuple[int, int]: """Returns the register index and (qu)bit index in this register for the mapped bit_index. @@ -255,9 +253,7 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit: # Try to decompose circuit before running # This is necessary for converting qiskit circuits with # custom packaged gates, e.g., QFT gates - circuit = circuit.decompose( - gates_to_decompose=["u1", "u2", "u3", "cx", "rx", "ry"] - ) + circuit = circuit.decompose(gates_to_decompose=["u3", "cx", "rx", "ry", "rz"]) circuit = circuit.decompose() mitiq_circuit = from_qasm(qasm2.dumps(circuit)) return mitiq_circuit From 7db464ffcfc8273c03f736ee72f22a72d365cfdd Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Wed, 20 Nov 2024 12:49:50 +0700 Subject: [PATCH 09/11] black --- mitiq/interface/mitiq_qiskit/conversions.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index 7dfe0fd85d..5630fa9ce3 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -52,7 +52,9 @@ def _remove_qasm_barriers(qasm: QASMType) -> QASMType: return "".join(lines) -def _map_bit_index(bit_index: int, new_register_sizes: List[int]) -> Tuple[int, int]: +def _map_bit_index( + bit_index: int, new_register_sizes: List[int] +) -> Tuple[int, int]: """Returns the register index and (qu)bit index in this register for the mapped bit_index. @@ -253,7 +255,9 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit: # Try to decompose circuit before running # This is necessary for converting qiskit circuits with # custom packaged gates, e.g., QFT gates - circuit = circuit.decompose(gates_to_decompose=["u3", "cx", "rx", "ry", "rz"]) + circuit = circuit.decompose( + gates_to_decompose=["u3", "cx", "rx", "ry", "rz"] + ) circuit = circuit.decompose() mitiq_circuit = from_qasm(qasm2.dumps(circuit)) return mitiq_circuit From 3b81eb28aa56ad3ae7d567e1b2fc3c91bd5df5b2 Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Fri, 22 Nov 2024 23:37:46 +0700 Subject: [PATCH 10/11] added QFT to gates_to_decompose --- mitiq/interface/mitiq_qiskit/conversions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index 5630fa9ce3..c2ee1ca772 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -249,17 +249,19 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit: Returns: Mitiq circuit representation equivalent to the input Qiskit circuit. """ + try: mitiq_circuit = from_qasm(qasm2.dumps(circuit)) except QasmException: # Try to decompose circuit before running # This is necessary for converting qiskit circuits with # custom packaged gates, e.g., QFT gates + gates_to_decompose = ["rxx", "rzz", "rzx", "ryy", "QFT"] circuit = circuit.decompose( - gates_to_decompose=["u3", "cx", "rx", "ry", "rz"] + gates_to_decompose=gates_to_decompose, reps=10 ) - circuit = circuit.decompose() mitiq_circuit = from_qasm(qasm2.dumps(circuit)) + return mitiq_circuit From 828fdf21bef5c0ae7439a13c89763b3b14eb60cf Mon Sep 17 00:00:00 2001 From: gluonhiggs Date: Sat, 23 Nov 2024 06:27:00 +0700 Subject: [PATCH 11/11] removed reps and changed constant name --- mitiq/interface/mitiq_qiskit/conversions.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mitiq/interface/mitiq_qiskit/conversions.py b/mitiq/interface/mitiq_qiskit/conversions.py index c2ee1ca772..77d62e2e8b 100644 --- a/mitiq/interface/mitiq_qiskit/conversions.py +++ b/mitiq/interface/mitiq_qiskit/conversions.py @@ -256,10 +256,8 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit: # Try to decompose circuit before running # This is necessary for converting qiskit circuits with # custom packaged gates, e.g., QFT gates - gates_to_decompose = ["rxx", "rzz", "rzx", "ryy", "QFT"] - circuit = circuit.decompose( - gates_to_decompose=gates_to_decompose, reps=10 - ) + GATES_TO_DECOMPOSE = ["rxx", "rzz", "rzx", "ryy", "QFT"] + circuit = circuit.decompose(gates_to_decompose=GATES_TO_DECOMPOSE) mitiq_circuit = from_qasm(qasm2.dumps(circuit)) return mitiq_circuit