Skip to content

Commit

Permalink
Add warning when circ is mixed + open
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilkhatri committed Nov 29, 2024
1 parent aa14336 commit 952f23f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lambeq/backend/pennylane.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from __future__ import annotations

from itertools import product
import sys
from typing import TYPE_CHECKING

import pennylane as qml
Expand Down Expand Up @@ -221,6 +222,12 @@ def to_pennylane(lambeq_circuit: Diagram, probabilities=False,
raise ValueError('Only pure circuits, or circuits with discards'
' are currently supported.')

if lambeq_circuit.is_mixed and lambeq_circuit.cod:
# Some qubits discarded, some left open
print('Warning: Circuit includes both discards and open codomain'
' wires. All open wires will be discarded during conversion',
file=sys.stderr)

tk_circ = lambeq_circuit.to_tk()
op_list, params_list, wires_list, symbols_set = (
extract_ops_from_tk(tk_circ)
Expand Down
10 changes: 10 additions & 0 deletions tests/backend/test_pennylane_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def test_pennylane_circuit_mixed_error():
snake.to_pennylane()


def test_pennylane_circuit_mixed_warning(capsys):
bell_state = Diagram.caps(qubit, qubit)
bell_discarded = bell_state >> Discard() @ Id(qubit)
_ = bell_discarded.to_pennylane()
captured = capsys.readouterr()
assert captured.err == ('Warning: Circuit includes both discards and open '
'codomain wires. All open wires will be discarded '
'during conversion\n')


def test_pennylane_circuit_draw(capsys):
bell_state = Diagram.caps(qubit, qubit)
bell_effect = bell_state[::-1]
Expand Down

0 comments on commit 952f23f

Please sign in to comment.