Skip to content

Commit

Permalink
More straight forward indices permutation
Browse files Browse the repository at this point in the history
  • Loading branch information
multiphaseCFD committed Sep 16, 2024
1 parent c32c399 commit 482d1ef
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pennylane_lightning/lightning_tensor/_tensornet.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,31 @@ def gate_matrix_decompose(gate_ops_matrix, wires, c_dtype):
# Create the correct order of indices for the gate tensor to be decomposed
indices_order = []
for i in range(len(wires)):
indices_order.extend([original_axes[i] + len(wires), original_axes[i]])
indices_order.extend([original_axes[i], original_axes[i] + len(wires)])

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L97-L99

Added lines #L97 - L99 were not covered by tests
# Reverse the indices order to match the requirement of cutensornet backend
indices_order.reverse()

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L101

Added line #L101 was not covered by tests

# Permutation of the gate tensor
gate_tensor = np.transpose(gate_tensor, axes=indices_order)

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L104

Added line #L104 was not covered by tests

mpo_site_shape = [2] * 2

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L106

Added line #L106 was not covered by tests
# TODO: Discuss if public interface for max_mpo_bond_dim argument
max_mpo_bond_dim = 2 ** len(wires) # Exact SVD decomposition for MPO

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L108

Added line #L108 was not covered by tests
# The indices order of MPOs: 1. left-most site: [bra, ket, bondR]; 2. right-most sites: [bondL, bra, ket]; 3. sites in-between: [bondL, bra, ket, bondR].
# The indices order of MPOs: 1. left-most site: [ket, bra, bondR]; 2. right-most sites: [bondL, ket, bra]; 3. sites in-between: [bondL, ket, bra, bondR].
MPOs = decompose_dense(gate_tensor, len(wires), mpo_site_shape, max_mpo_bond_dim)

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L110

Added line #L110 was not covered by tests

# Convert the MPOs to the correct order for the cutensornet backend
mpos = []
for i in range(len(MPOs)):

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

View check run for this annotation

codefactor.io / CodeFactor

pennylane_lightning/lightning_tensor/_tensornet.py#L114

Consider using enumerate instead of iterating with range and len (consider-using-enumerate)
if i == 0:

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L113-L115

Added lines #L113 - L115 were not covered by tests
# [bond, bra, ket](0, 1, 2) -> [bra, bond, ket](1, 0, 2) -> Fortran order or reverse indices(2, 0, 1) to match the order requirement of cutensornet backend.
mpos.append(np.transpose(MPOs[len(MPOs) - 1 - i], axes=(2, 0, 1)))
# [ket, bra, bond](0, 1, 2) -> [ket, bond, bra](0, 2, 1) -> Fortran order or reverse indices(1, 2, 0) to match the order requirement of cutensornet backend.
mpos.append(np.transpose(MPOs[i], axes=(1, 2, 0)))
elif i == len(MPOs) - 1:

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L117-L118

Added lines #L117 - L118 were not covered by tests
# [bra, ket, bond](0, 1, 2) -> [bond, bra, ket](2, 0, 1) -> Fortran order or reverse indices(1, 0, 2) to match the order requirement of cutensornet backend.
mpos.append(np.transpose(MPOs[len(MPOs) - 1 - i], axes=(1, 0, 2)))
# [bond, ket, bra](0, 1, 2) -> Fortran order or reverse indices(2, 1, 0) to match the order requirement of cutensornet backend.
mpos.append(np.transpose(MPOs[i], axes=(2, 1, 0)))

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L120

Added line #L120 was not covered by tests
else:
# [bondL, bra, ket, bondR](0, 1, 2, 3) -> [bondL, bra, bondR, ket]->(0, 1, 3, 2) -> [bondR, bra, bondL, ket](3, 1, 0, 2) -> Fortran order or reverse indices(2, 0, 1, 3) to match the requirement of cutensornet backend.
mpos.append(np.transpose(MPOs[len(MPOs) - 1 - i], axes=(2, 0, 1, 3)))
# [bondL, ket, bra, bondR](0, 1, 2, 3) -> [bondL, ket, bondR, bra](0, 1, 3, 2) -> Fortran order or reverse indices(2, 3, 1, 0) to match the requirement of cutensornet backend.
mpos.append(np.transpose(MPOs[i], axes=(2, 3, 1, 0)))

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L123

Added line #L123 was not covered by tests

return mpos, sorted_wires

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

View check run for this annotation

Codecov / codecov/patch

pennylane_lightning/lightning_tensor/_tensornet.py#L125

Added line #L125 was not covered by tests

Expand Down

0 comments on commit 482d1ef

Please sign in to comment.