Skip to content

Commit

Permalink
Merged with recent pennylane pr
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewilsonquantinuum committed Dec 3, 2024
1 parent f779122 commit 7e55448
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lambeq/backend/pennylane.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import pennylane as qml
import sympy
import torch
import sys

from lambeq.backend.quantum import (circuital_to_dict,
is_circuital,
Expand Down Expand Up @@ -165,6 +166,15 @@ def to_pennylane(diagram: Diagram, probabilities=False,
The PennyLane circuit equivalent to the input lambeq circuit.
"""
if any(isinstance(box, Measure) for box in diagram.boxes):
raise ValueError('Only pure circuits, or circuits with discards'
' are currently supported.')

if diagram.is_mixed and diagram.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)

if not is_circuital(diagram):
diagram = to_circuital(diagram)
Expand Down
7 changes: 4 additions & 3 deletions lambeq/backend/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,9 +1249,10 @@ def to_circuital(diagram: Diagram):
circuit = circuit.init_and_discard()

# Cleans up any '1' kets and converts them to X|0> -> |1>
def remove_ket1(_, box: Box) -> Diagram | Box:
def remove_ketbra1(_, box: Box) -> Diagram | Box:
ob_map: dict[Box, Diagram]
ob_map = {Ket(1): Ket(0) >> X} # type: ignore[dict-item]
ob_map = {Ket(1): Ket(0) >> X,
Bra(1): X >> Ket(0)} # type: ignore[dict-item]
return ob_map.get(box, box)

def add_qubit(qubits: list[Layer],
Expand Down Expand Up @@ -1459,7 +1460,7 @@ def build_left_right(q_idx, layer, layers):

circuit = Functor(target_category=quantum, # type: ignore [assignment]
ob=lambda _, x: x,
ar=remove_ket1)(circuit) # type: ignore [arg-type]
ar=remove_ketbra1)(circuit) # type: ignore [arg-type]

layers = circuit.layers

Expand Down

0 comments on commit 7e55448

Please sign in to comment.