Skip to content

Commit

Permalink
fix: Warn user about implicit qubit permutations in tk_to_qiskit (#412
Browse files Browse the repository at this point in the history
)

* fix: warn user if Circuit contains IQP

* add test

* add changelog entry

* remove unused import

* organise imports for ruff

* fix a typo

* improve `_has_implicit_permutation` function

Co-authored-by: Alec Edgington <[email protected]>

* fix typo

---------

Co-authored-by: Alec Edgington <[email protected]>
  • Loading branch information
CalMacCQ and cqc-alec authored Nov 8, 2024
1 parent b332fbf commit ffbf31a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Unreleased

- Fix handling of non-default registers when selecting bits in results.
- The {py:func}`tk_to_qiskit` converter gives a warning if the input {py:class}`~pytket.circuit.Circuit` contains [implicit qubit permutations](https://docs.quantinuum.com/tket/user-guide/manual/manual_circuit.html#implicit-qubit-permutations).

## 0.58.0 (October 2024)

Expand Down
14 changes: 14 additions & 0 deletions pytket/extensions/qiskit/qiskit_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

"""Methods to allow conversion between Qiskit and pytket circuit classes
"""
import warnings
from collections import defaultdict
from collections.abc import Iterable
from inspect import signature
Expand Down Expand Up @@ -855,6 +856,10 @@ def append_tk_command_to_qiskit(
supported_gate_rebase = AutoRebase(_protected_tket_gates)


def _has_implicit_permutation(circ: Circuit) -> bool:
return any(q0 != q1 for q0, q1 in circ.implicit_qubit_permutation().items())


def tk_to_qiskit(
tkcirc: Circuit, replace_implicit_swaps: bool = False
) -> QuantumCircuit:
Expand All @@ -873,6 +878,15 @@ def tk_to_qiskit(
tkc = tkcirc.copy() # Make a local copy of tkcirc
if replace_implicit_swaps:
tkc.replace_implicit_wire_swaps()

if _has_implicit_permutation(tkcirc) and not replace_implicit_swaps:
warnings.warn(
"The pytket Circuit contains implicit qubit permutations"
+ " which aren't handled by default."
+ " Consider using the replace_implicit_swaps flag in tk_to_qiskit or"
+ " replacing them using Circuit.replace_implicit_swaps()."
)

qcirc = QuantumCircuit(name=tkc.name)
qreg_sizes: dict[str, int] = {}
for qb in tkc.qubits:
Expand Down
7 changes: 7 additions & 0 deletions tests/qiskit_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,10 @@ def test_nonregister_bits() -> None:
c.rename_units({Bit(0): Bit(1)})
with pytest.raises(NotImplementedError):
tk_to_qiskit(c)


def test_implicit_swap_warning() -> None:
c = Circuit(2).H(0).SWAP(0, 1)
c.replace_SWAPs()
with pytest.warns(UserWarning, match="The pytket Circuit contains implicit qubit"):
tk_to_qiskit(c)

0 comments on commit ffbf31a

Please sign in to comment.