Skip to content

Commit

Permalink
AerUnitaryBackend: call transpile before run (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Aug 29, 2023
1 parent 74208fd commit 3a05202
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 6 additions & 0 deletions pytket/extensions/qiskit/backends/aer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)

import numpy as np
from qiskit import transpile # type: ignore
from qiskit.providers.aer.noise import NoiseModel # type: ignore
from qiskit.quantum_info.operators import Pauli as qk_Pauli # type: ignore
from qiskit.quantum_info.operators.symplectic.sparse_pauli_op import SparsePauliOp # type: ignore
Expand Down Expand Up @@ -110,6 +111,7 @@ class _AerBaseBackend(Backend):
_required_predicates: List[Predicate]
_noise_model: Optional[NoiseModel] = None
_has_arch: bool = False
_needs_transpile: bool = False

@property
def required_predicates(self) -> List[Predicate]:
Expand Down Expand Up @@ -248,6 +250,9 @@ def process_circuits(
qc.save_unitary()
qcs.append(qc)

if self._needs_transpile:
qcs = transpile(qcs, self._qiskit_backend)

seed = cast(Optional[int], kwargs.get("seed"))
job = self._qiskit_backend.run(
qcs,
Expand Down Expand Up @@ -539,6 +544,7 @@ class AerUnitaryBackend(_AerBaseBackend):

_memory = False
_noise_model = None
_needs_transpile = True

_qiskit_backend_name = "aer_simulator_unitary"

Expand Down
20 changes: 19 additions & 1 deletion tests/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from qiskit_ibm_provider import IBMProvider # type: ignore

from pytket.circuit import Circuit, OpType, BasisOrder, Qubit, reg_eq, Unitary2qBox # type: ignore
from pytket.circuit import Circuit, OpType, BasisOrder, Qubit, reg_eq, Unitary2qBox, QControlBox, CircBox # type: ignore
from pytket.passes import CliffordSimp # type: ignore
from pytket.pauli import Pauli, QubitPauliString # type: ignore
from pytket.predicates import CompilationUnit, NoMidMeasurePredicate # type: ignore
Expand Down Expand Up @@ -1230,3 +1230,21 @@ def test_ecr_gate_compilation(ibm_sherbrooke_backend: IBMQBackend) -> None:
circ, optimisation_level
)
assert ibm_sherbrooke_backend.valid_circuit(compiled_circ)


def test_unitary_backend_transpiles() -> None:
"""regression test for https://github.com/CQCL/pytket-qiskit/issues/142"""
backend = AerUnitaryBackend()
n_ancillas = 5 # using n_ancillas <=4 doees not raise an error
n_spins = 1
circ = Circuit(n_ancillas + n_spins)
trgt = Circuit(n_spins)
trgt.X(0)

circ.add_qcontrolbox(
QControlBox(CircBox(trgt), n_ancillas), list(range(n_ancillas + n_spins))
)

compiled_circ = backend.get_compiled_circuit(circ, optimisation_level=0)
# using optimisation_level >= 1 does not raise an error
backend.run_circuit(compiled_circ)
10 changes: 0 additions & 10 deletions tests/qiskit_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from qiskit import QuantumCircuit, execute # type: ignore
from qiskit.opflow import CircuitStateFn, CircuitSampler # type: ignore
from qiskit.providers import JobStatus # type: ignore
from qiskit.utils import QuantumInstance # type: ignore
from qiskit.algorithms import Grover, AmplificationProblem # type: ignore
from qiskit.transpiler.exceptions import TranspilerError # type: ignore
Expand Down Expand Up @@ -107,15 +106,6 @@ def test_unitary() -> None:
assert np.allclose(u, u2)


def test_cancel() -> None:
b = AerBackend()
tb = TketBackend(b)
qc = circuit_gen()
job = execute(qc, tb, shots=1024)
job.cancel()
assert job.status() in [JobStatus.CANCELLED, JobStatus.DONE]


@pytest.mark.skipif(skip_remote_tests, reason=REASON)
def test_qiskit_counts(belem_emulator_backend: IBMQEmulatorBackend) -> None:
num_qubits = 2
Expand Down

0 comments on commit 3a05202

Please sign in to comment.