Skip to content

Commit

Permalink
tidy up python layer
Browse files Browse the repository at this point in the history
  • Loading branch information
multiphaseCFD committed Sep 13, 2024
1 parent b1f332b commit b5a858c
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions pennylane_lightning/lightning_tensor/_tensornet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
# pylint: disable=ungrouped-imports
from pennylane_lightning.core._serialize import global_phase_diagonal

Check notice on line 34 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane_lightning/lightning_tensor/_tensornet.py#L34

Unused global_phase_diagonal imported from pennylane_lightning.core._serialize (unused-import)

def is_sorted(arr):
"""Check if an array is sorted."""
return all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1))

def svd_split(Mat, site_shape, max_bond_dim):
"""SVD decomposition of a matrix via numpy linalg. Note that this function is to be moved to the C++ layer."""
Expand Down Expand Up @@ -76,8 +73,9 @@ def decompose_dense(psi, n_wires, site_shape, max_bond_dim):

return Ms

def gate_matrix_permutation(gate_ops_matrix, wires, c_dtype):
# Permute the gate matrix to match the wire order in the tensor network

def gate_matrix_decompose(gate_ops_matrix, wires, c_dtype):
"""Permute and decompose a gate matrix into MPO sites."""
sorted_indexed_wires = sorted(enumerate(wires), key=lambda x: x[1])

Check warning on line 79 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L79

Added line #L79 was not covered by tests

sorted_wires = []
Expand Down Expand Up @@ -117,11 +115,12 @@ def gate_matrix_permutation(gate_ops_matrix, wires, c_dtype):
# sites between MSB and LSB [bondL, bra, ket, bondR] -> [ket, bondL, bra, bondR]
# To match the order of cutensornet backend
mpos.append(np.transpose(MPOs[len(MPOs) - 1 - i], axes=(2, 0, 1, 3)))

Check warning on line 117 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L117

Added line #L117 was not covered by tests

return mpos, sorted_wires

Check warning on line 119 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L119

Added line #L119 was not covered by tests


def create_swap_queue(wires):
"""Create a swap ops queue non-local target wires gates applied to the MPS tensor network."""
swap_wire_pairs = []
wires_size = len(wires)
if (wires[-1] - wires[0]) == wires_size - 1:
Expand All @@ -140,23 +139,24 @@ def create_swap_queue(wires):
if left_pos >= 0:
wire_pair_queue = []
print()
for i in range(wires[left_pos], wires[fixed_pos]-(fixed_pos - left_pos)):
wire_pair_queue.append([i, i+1])
for i in range(wires[left_pos], wires[fixed_pos] - (fixed_pos - left_pos)):
wire_pair_queue.append([i, i + 1])
if wire_pair_queue:
op_wires_queue.append(wire_pair_queue)
op_wires_queue.append(wire_pair_queue)
target_wires = [target_wires[0] - 1] + target_wires
left_pos -= 1

Check warning on line 147 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L138-L147

Added lines #L138 - L147 were not covered by tests

if right_pos < wires_size:
wire_pair_queue = []
for i in range(wires[right_pos], wires[fixed_pos]+ right_pos - fixed_pos, -1):
wire_pair_queue.append([i, i-1])
for i in range(wires[right_pos], wires[fixed_pos] + right_pos - fixed_pos, -1):
wire_pair_queue.append([i, i - 1])
if wire_pair_queue:
op_wires_queue.append(wire_pair_queue)
target_wires += [target_wires[-1] + 1]
target_wires += [target_wires[-1] + 1]
right_pos += 1
return target_wires, op_wires_queue

Check warning on line 157 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L149-L157

Added lines #L149 - L157 were not covered by tests


# pylint: disable=too-many-instance-attributes
class LightningTensorNet:
"""Lightning tensornet class.
Expand Down Expand Up @@ -316,7 +316,7 @@ def _apply_basis_state(self, state, wires):
raise ValueError("BasisState parameter and wires must be of equal length.")

self._tensornet.setBasisState(state)

def _apply_MPO(self, gate_matrix, wires):
"""Apply a matrix product operator to the quantum state.
Expand All @@ -327,11 +327,13 @@ def _apply_MPO(self, gate_matrix, wires):
None
"""
# Get sorted wires and MPO site tensor
mpos, sorted_wires = gate_matrix_permutation(gate_matrix, wires, self._c_dtype)
mpos, sorted_wires = gate_matrix_decompose(gate_matrix, wires, self._c_dtype)

Check warning on line 330 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L330

Added line #L330 was not covered by tests

# Check if SWAP operation should be applied
local_target_wires, swap_pair_queue = create_swap_queue(sorted_wires)

Check warning on line 333 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L333

Added line #L333 was not covered by tests

# TODO: This following part can be moved to the C++ layer in 2024 Q4
# Apply SWAP operation to ensure the target wires are local
for swap_wire_pairs in swap_pair_queue:
for swap_wires in swap_wire_pairs:
swap_op = getattr(self._tensornet, "SWAP", None)
Expand All @@ -341,6 +343,7 @@ def _apply_MPO(self, gate_matrix, wires):

self._tensornet.applyMPOOperator(mpos, local_target_wires, max_mpo_bond_dim)

Check warning on line 344 in pennylane_lightning/lightning_tensor/_tensornet.py

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L344

Added line #L344 was not covered by tests

# Apply SWAP operation to restore the original wire order
for swap_wire_pairs in swap_pair_queue[::-1]:
for swap_wires in swap_wire_pairs[::-1]:
swap_op = getattr(self._tensornet, "SWAP", None)
Expand Down

0 comments on commit b5a858c

Please sign in to comment.