Skip to content

Commit

Permalink
Issue #2354 Fix - qiskit QFT gates error during conversion (#2404)
Browse files Browse the repository at this point in the history
* Fixed issue #2354 where a fallback was addded to conversions.py to decompose a qiskit circuit to native gates if an error occured. A test with zne was added to test for this with the QFT gate.

* added myself to the bottom of the authors file

* Adjusted code to follow the comments that cosenal placed with previous pull request.
Moved the exception catch from the from_qiskit() to the from_qasm() function as that function is
more general and is called by from_qiskit().
Used a QasmException catch instead of Exception to make it more clear that Qasm has trouble converting
Adjusted code to follow the comments that cosenal placed with previous pull request.
Moved the exception catch from the from_qiskit() to the from_qasm() function as that function is
more general and is called by from_qiskit().
Used a QasmException catch instead of Exception to make it more clear that Qasm has trouble converting
different gates. Removed choosing a basis for the transpile and let qiskit decide what gates to use.
Added an additional test case to test the from_qasm() function.

* resolving second round of changes reviewed by cosenal! moved the exception catch to the from_qiskit() function from the from_qasm() function as from_qasm() can handle non-qiskit circuits. Switched the test case to test the from_qiskit() function in doing so. Also only decomposed rather than transpiling and decomposing as transpiling doesn't do anything.

* renaming and added updated docstrings according to cosenal suggestions
  • Loading branch information
NnguyenHTommy committed Jun 11, 2024
1 parent e0eff03 commit 720079d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ Yash Prabhat
Vladimir Kozhukalov
Francesc Sabater
Emiliano Godinez
Tommy Nguyen
11 changes: 10 additions & 1 deletion mitiq/interface/mitiq_qiskit/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np
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.transpiler import PassManager
from qiskit.transpiler.layout import Layout
Expand Down Expand Up @@ -248,7 +249,15 @@ def from_qiskit(circuit: qiskit.QuantumCircuit) -> cirq.Circuit:
Returns:
Mitiq circuit representation equivalent to the input Qiskit circuit.
"""
return from_qasm(qasm2.dumps(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
circuit = circuit.decompose()
mitiq_circuit = from_qasm(qasm2.dumps(circuit))
return mitiq_circuit


def from_qasm(qasm: QASMType) -> cirq.Circuit:
Expand Down
13 changes: 13 additions & 0 deletions mitiq/interface/mitiq_qiskit/tests/test_conversions_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ def test_random_circuit_to_from_qasm():
)


def test_convert_with_qft():
"""Tests converting a Qiskit circuit with a QFT to a Cirq circuit."""
circuit = qiskit.QuantumCircuit(1)
circuit &= qiskit.circuit.library.QFT(1)
circuit.measure_all()
qft_cirq = from_qiskit(circuit)
qreg = cirq.LineQubit.range(1)
cirq_circuit = cirq.Circuit(
[cirq.ops.H.on(qreg[0]), cirq.ops.measure(qreg[0], key="meas")]
)
assert _equal(cirq_circuit, qft_cirq)


@pytest.mark.parametrize("as_qasm", (True, False))
def test_convert_with_barrier(as_qasm):
"""Tests converting a Qiskit circuit with a barrier to a Cirq circuit."""
Expand Down
20 changes: 20 additions & 0 deletions mitiq/zne/tests/test_zne.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np
import pytest
import qiskit
import qiskit.circuit
from qiskit_aer import AerSimulator

from mitiq import QPROGRAM, SUPPORTED_PROGRAM_TYPES
Expand Down Expand Up @@ -540,3 +541,22 @@ def execute(circuit: qiskit.QuantumCircuit, shots: int = 8192) -> float:
# Note: Unmitigated value is also (usually) within 10% of the true value.
# This is more to test usage than effectiveness.
assert abs(zne_value - true_value) < 0.1


def test_execute_zne_on_qiskit_circuit_with_QFT():
"""Tests ZNE of a Qiskit device with a QFT gate."""

def qs_noisy_simulation(
circuit: qiskit.QuantumCircuit, shots: int = 1
) -> float:
noise_model = initialized_depolarizing_noise(noise_level=0.02)
backend = AerSimulator(noise_model=noise_model)
job = backend.run(circuit.decompose(), shots=shots)
return job.result().get_counts().get("0", 0.0) / shots

circuit = qiskit.QuantumCircuit(1)
circuit &= qiskit.circuit.library.QFT(1)
circuit.measure_all()

mitigated = execute_with_zne(circuit, qs_noisy_simulation)
assert abs(mitigated) < 1000

0 comments on commit 720079d

Please sign in to comment.