Skip to content

Commit

Permalink
feat: using int indices in place of letters
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Sep 24, 2024
1 parent 71d53aa commit 591a736
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/qibo/quantum_info/basis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from itertools import product
from string import ascii_letters
from typing import Optional

import numpy as np
Expand Down Expand Up @@ -93,15 +92,18 @@ def pauli_basis(

pauli_labels = {"I": matrices.I, "X": matrices.X, "Y": matrices.Y, "Z": matrices.Z}
basis_single = backend.cast([pauli_labels[label] for label in pauli_order])
if backend.name == "tensorflow":
einsum = np.einsum
else:
einsum = backend.np.einsum

if nqubits > 1:
dim = 2**nqubits
letters = [ascii_letters[3 * i : 3 * (i + 1)] for i in range(nqubits)]
lhs = ",".join(letters)
rhs = "".join([letter for group in zip(*letters) for letter in group])
basis_full = backend.np.einsum(
f"{lhs}->{rhs}", *[basis_single for _ in range(nqubits)]
).reshape(4**nqubits, dim, dim)
input_indices = [range(3 * i, 3 * (i + 1)) for i in range(nqubits)]
output_indices = (i for indices in zip(*input_indices) for i in indices)
operands = [basis_single for _ in range(nqubits)]
inputs = [item for pair in zip(operands, input_indices) for item in pair]
basis_full = einsum(*inputs, output_indices).reshape(4**nqubits, dim, dim)
else:
basis_full = basis_single

Expand Down

0 comments on commit 591a736

Please sign in to comment.