Skip to content

Commit

Permalink
Fixed SWAPs
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewilsonquantinuum committed Dec 10, 2024
1 parent 330537e commit 3a56070
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lambeq/backend/pennylane.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
'CU1': lambda a, wires: qml.ctrl(qml.U1(a,
wires=wires[1]),
control=wires[0]),
'Swap': qml.SWAP,
'SWAP': qml.SWAP,
'noop': qml.Identity,
}

Expand Down
27 changes: 15 additions & 12 deletions lambeq/backend/quantum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,14 +1359,15 @@ def pull_qubit_through(q_idx: int,
Inserts a qubit type into every layer at the appropriate index
q_idx: idx - index of where to insert the gate.
"""

new_gates = []
for gate_layer in gates:

l_size = len(gate_layer.left)

# Inserting to the left is always trivial
if q_idx <= l_size:
gate_layer.left = gate_layer.left.insert(dom, q_idx)
new_gates.append(gate_layer)
# Qubit on right of gate. Handles 1 qubit gates by l(dom) = 1
elif q_idx > l_size + len(gate_layer.box.dom) - 1:

Expand All @@ -1377,6 +1378,7 @@ def pull_qubit_through(q_idx: int,
gate_layer.right = gate_layer.right.insert(dom, r_rel)

q_idx = r_rel + l_size + len(gate_layer.box.cod)
new_gates.append(gate_layer)

else:
if isinstance(gate_layer.box, Controlled):
Expand Down Expand Up @@ -1409,23 +1411,24 @@ def pull_qubit_through(q_idx: int,

box.dom = box.dom.insert(dom, q_idx - l_size)
box.cod = box.cod.insert(dom, q_idx - l_size)
new_gates.append(gate_layer)

if isinstance(gate_layer.box, Swap):

# Replace single swap with a series of swaps
# Swaps are 2 wide, so if a qubit is pulled through we
# have to use the pulled qubit as an temp ancillary.
gates.append(Layer(gate_layer.left,
Swap(qubit, qubit),
dom >> gate_layer.right))
gates.append(Layer(dom >> gate_layer.left,
Swap(qubit, qubit),
gate_layer.right))
gates.append(Layer(gate_layer.left,
Swap(qubit, qubit),
dom >> gate_layer.right))

return gates, q_idx
new_gates.append(Layer(gate_layer.left,
Swap(qubit, qubit),
dom >> gate_layer.right))
new_gates.append(Layer(dom >> gate_layer.left,
Swap(qubit, qubit),
gate_layer.right))
new_gates.append(Layer(gate_layer.left,
Swap(qubit, qubit),
dom >> gate_layer.right))

return new_gates, q_idx

def build_left_right(q_idx, layer, layers):
"""
Expand Down

0 comments on commit 3a56070

Please sign in to comment.