Skip to content

Commit

Permalink
Fixed issue unitaryfund#2354 where a fallback was addded to conversio…
Browse files Browse the repository at this point in the history
…ns.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.
  • Loading branch information
NnguyenHTommy committed Jun 6, 2024
1 parent 837ea83 commit a0f4fbc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
14 changes: 12 additions & 2 deletions mitiq/interface/mitiq_qiskit/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy as np
import qiskit
from cirq.contrib.qasm_import import circuit_from_qasm
from qiskit import qasm2
from qiskit import compiler, qasm2
from qiskit.transpiler import PassManager
from qiskit.transpiler.layout import Layout
from qiskit.transpiler.passes import SetLayout
Expand Down Expand Up @@ -248,7 +248,17 @@ 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 Exception:
# If the conversion fails, try to decompose and transpile the
# circuit to native gates
circuit = compiler.transpile(
circuit, basis_gates=["u1", "u2", "u3", "cx"]
)
circuit = circuit.decompose()
mitiq_circuit = from_qasm(qasm2.dumps(circuit))
return mitiq_circuit


def from_qasm(qasm: QASMType) -> 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_transpiled_qiskit_circuit_with_noise_and_QFT():
"""Tests ZNE when transpiling to 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 a0f4fbc

Please sign in to comment.