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 Sep 7, 2023
1 parent 00ee975 commit b2c7adb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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 @@ -122,6 +123,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 @@ -268,6 +270,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 @@ -584,6 +589,7 @@ class AerUnitaryBackend(_AerBaseBackend):

_memory = False
_noise_model = None
_needs_transpile = True

_qiskit_backend_name = "aer_simulator_unitary"

Expand Down
23 changes: 22 additions & 1 deletion tests/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from qiskit_aer import Aer # type: ignore
from qiskit_ibm_provider.exceptions import IBMError # 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 @@ -1374,3 +1374,24 @@ def test_unitary_sim_gateset() -> None:
unitary_sim_gateset = backend.backend_info.gate_set
unsupported_ops = {OpType.Reset, OpType.Measure, OpType.Conditional}
assert unitary_sim_gateset.isdisjoint(unsupported_ops)


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
r = backend.run_circuit(compiled_circ)
u = r.get_unitary()
# check that the lower-right 2x2 submatrix of the unitary is the matrix of the X gate.
assert np.isclose(u[62:64, 62:64], np.asarray(([0.0, 1.0], [1.0, 0.0]))).all()

0 comments on commit b2c7adb

Please sign in to comment.