Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚸 slightly tweak how optional qiskit dependency is handled #797

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/mqt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def load(input_circuit: QuantumComputation | str | os.PathLike[str] | QuantumCir
The :class:`~mqt.core.ir.QuantumComputation`.

Raises:
ValueError: If the input circuit is a Qiskit :class:`~qiskit.circuit.QuantumCircuit`,
but the `qiskit` extra is not installed.
FileNotFoundError: If the input circuit is a file name and the file does not exist.
"""
if isinstance(input_circuit, QuantumComputation):
Expand All @@ -54,11 +52,8 @@ def load(input_circuit: QuantumComputation | str | os.PathLike[str] | QuantumCir

return QuantumComputation(input_str)

try:
from .plugins.qiskit import qiskit_to_mqt
except ImportError:
msg = "Qiskit is not installed. Please install `mqt.core[qiskit]` to use Qiskit circuits as input."
raise ValueError(msg) from None
# At this point, we know that the input is a Qiskit QuantumCircuit
from .plugins.qiskit import qiskit_to_mqt

return qiskit_to_mqt(input_circuit)

Expand Down
4 changes: 2 additions & 2 deletions src/mqt/core/plugins/qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import warnings
from typing import TYPE_CHECKING, cast

from qiskit.circuit import AncillaQubit, AncillaRegister, Clbit, Instruction, ParameterExpression, Qubit
from qiskit.circuit import AncillaQubit, AncillaRegister, Clbit, Qubit

from ..ir import QuantumComputation
from ..ir.operations import (
Expand All @@ -29,7 +29,7 @@
if TYPE_CHECKING:
from collections.abc import Mapping, Sequence

from qiskit.circuit import QuantumCircuit
from qiskit.circuit import Instruction, ParameterExpression, QuantumCircuit


def qiskit_to_mqt(circ: QuantumCircuit) -> QuantumComputation:
Expand Down
17 changes: 4 additions & 13 deletions test/python/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

from __future__ import annotations

import sys
from pathlib import Path

import pytest
from qiskit import QuantumCircuit, qasm2

from mqt.core import load
from mqt.core.ir import QuantumComputation

Expand Down Expand Up @@ -92,11 +88,14 @@ def test_loading_nonexistent_file() -> None:

def test_loading_qiskit_circuit() -> None:
"""Test whether importing a Qiskit circuit works."""
from qiskit import QuantumCircuit
from qiskit.qasm2 import dumps

qiskit_circuit = QuantumCircuit(2, 2)
qiskit_circuit.h(0)
qiskit_circuit.cx(0, 1)
qiskit_circuit.measure(range(2), range(2))
qasm = qasm2.dumps(qiskit_circuit)
qasm = dumps(qiskit_circuit)

# load the circuit
qc = load(qiskit_circuit)
Expand All @@ -110,14 +109,6 @@ def test_loading_qiskit_circuit() -> None:
assert "".join(qasm.split()) in "".join(qc_qasm.split())


def test_qiskit_import_error(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that trying to import a Qiskit circuit without the `qiskit` extra raises an error."""
monkeypatch.setitem(sys.modules, "mqt.core.plugins.qiskit", None)

with pytest.raises(ValueError, match="Qiskit is not installed"):
load(QuantumCircuit())


def test_loading_qasm2_string() -> None:
"""Test whether importing a QASM2 string works."""
qasm = 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\ncreg c[2];\nh q[0];\ncx q[0], q[1];\nmeasure q -> c;\n'
Expand Down
4 changes: 2 additions & 2 deletions test/python/test_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from typing import cast

import pytest
from qiskit import QuantumCircuit, transpile
from qiskit.circuit import AncillaRegister, ClassicalRegister, Parameter, QuantumRegister
from qiskit import transpile
from qiskit.circuit import AncillaRegister, ClassicalRegister, Parameter, QuantumCircuit, QuantumRegister
from qiskit.circuit.library import U2Gate, XXMinusYYGate, XXPlusYYGate
from qiskit.providers.fake_provider import GenericBackendV2

Expand Down
Loading