Skip to content

Commit

Permalink
changes to be compatible with pennylane 0.36.0
Browse files Browse the repository at this point in the history
  • Loading branch information
boucherlfg committed Feb 1, 2025
1 parent a788570 commit 02a5f0b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
56 changes: 56 additions & 0 deletions pennylane_calculquebec/factorizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pennylane.tape import QuantumTape
import pennylane as qml
import numpy as np

def get_label(value, num_wires):
result = []
for _ in range(int(num_wires)):
if value <= 0:
result.insert(0, 0)
else:
result.insert(0, 1 if (value & 1 == 1) else 0)
value >>= 1
return result

def same(a, b, tol = 1e-5):
return abs(a - b) < tol


def compare_result(result1, result2):
num_wires = np.log2(len(result1))
dissimilar_results = []

for i, (a, b) in enumerate(zip(result1, result2)):
if a == 0 and b == 0:
continue
if same(a, b):
continue

dissimilar_results.append(i)

connected = [0 for _ in range(int(num_wires))]
for dissimilar in dissimilar_results:
label = get_label(dissimilar, num_wires)
connected = [a ^ b for a, b in zip(label, connected)]

return dissimilar_results

def get_groups(tape : QuantumTape):
dev = qml.device("default.qubit")

result = qml.execute([tape], dev)[0]

groups = {}

for wire in tape.wires:
test_tape = type(tape)([qml.PauliX(wire)] + tape.operations, tape.measurements)
test_result = qml.execute([test_tape], dev)[0]
connected = compare_result(result, test_result)
groups[wire] = connected

return groups

tape = QuantumTape([qml.Hadamard(0), qml.PauliX(1), qml.CNOT([0, 2])], [qml.probs()])

groups = get_groups(tape)
print(groups)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pennylane_calculquebec.utility.optimization import find_previous_gate, find_next_gate
import pennylane.transforms as transforms
import numpy as np
from pennylane.ops.op_math.adjoint import adjoint, Adjoint

def remove_root_zs(tape : QuantumTape, iterations = 3) -> QuantumTape:
"""
Expand Down Expand Up @@ -45,18 +46,30 @@ def remove_leaf_zs(tape : QuantumTape, iterations = 3) -> QuantumTape:
break
return type(tape)(new_operations, tape.measurements, tape.shots)


def _get_adjoint_base(op):
isAdjoint = False
while(isinstance(op, Adjoint)):
isAdjoint = not isAdjoint
op = op.base
return op, isAdjoint


def _remove_trivials(tape : QuantumTape, iteration = 3, epsilon = 1E-8):
"""
removes 0rad rotations and identities
"""
new_operations = []
for op in tape.operations:
op, isAdjoint = _get_adjoint_base(op)

if len(op.parameters) > 0:
angle = op.parameters[0]
while angle > 2 * np.pi - epsilon: angle -= 2 * np.pi
while angle < 0: angle += 2 * np.pi
if abs(angle) > epsilon:
new_operations.append(type(op)(angle, wires=op.wires))
op = (type(op) if not isAdjoint else adjoint(type(op)))(angle, wires=op.wires)
new_operations.append(op)
elif op.name == "Identity":
continue
else:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_decompose_readout.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ def test_execute(mock_get_ops_for_product):
mock_get_ops_for_product.assert_not_called()

# observable Z @ Z
mock_get_ops_for_product.return_value = ["success"]
obs = qml.PauliX(0)
mock_get_ops_for_product.return_value = [obs]
tape = QuantumTape([], [qml.counts(qml.PauliZ(0) @ qml.PauliZ(0))])
tape = step.execute(tape)
assert len(tape.operations) == 1 and len(tape.measurements) == 1
assert tape.operations[0] == "success"
assert tape.operations[0] is obs
mock_get_ops_for_product.assert_called_once()

# pas d'observable
Expand Down

0 comments on commit 02a5f0b

Please sign in to comment.