Skip to content

Commit

Permalink
remove alias
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Oct 17, 2024
1 parent 8c6e162 commit 403621b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/qibo/quantum_info/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,14 @@ def pauli_basis(
pauli_labels = {"I": matrices.I, "X": matrices.X, "Y": matrices.Y, "Z": matrices.Z}
dim = 2**nqubits
basis_single = backend.cast([pauli_labels[label] for label in pauli_order])
einsum = np.einsum if backend.platform == "tensorflow" else backend.np.einsum

if nqubits > 1:
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 = backend.np.einsum(*inputs, output_indices).reshape(
4**nqubits, dim, dim
)
basis_full = einsum(*inputs, output_indices).reshape(4**nqubits, dim, dim)
else:
basis_full = basis_single

Expand All @@ -111,7 +110,7 @@ def pauli_basis(
else:
nonzero = backend.np.nonzero
basis = vectorization(basis_full, order=order, backend=backend)
indices = nonzero(basis)
indices = nonzero(backend.np.abs(basis)) # abs needed because of ``tensorflow``
basis = basis[indices].reshape(-1, dim)
indices = indices[1].reshape(-1, dim)

Expand Down
16 changes: 3 additions & 13 deletions tests/test_quantum_info_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,11 @@ def test_pauli_basis(
for elem_test, ind_test, elem, ind in zip(
elements, indexes, basis[0], basis[1]
):
backend.assert_allclose(
np.linalg.norm(backend.to_numpy(elem_test - elem)) < PRECISION_TOL,
True,
)
backend.assert_allclose(
np.linalg.norm(backend.to_numpy(ind_test - ind)) < PRECISION_TOL,
True,
)
backend.assert_allclose(elem_test, elem, atol=PRECISION_TOL)
backend.assert_allclose(ind_test, ind, atol=PRECISION_TOL)
else:
for pauli, pauli_test in zip(basis, basis_test):
backend.assert_allclose(
np.linalg.norm(backend.to_numpy(pauli - pauli_test))
< PRECISION_TOL,
True,
)
backend.assert_allclose(pauli, pauli_test, atol=PRECISION_TOL)

comp_basis_to_pauli(nqubits, normalize, sparse, order, pauli_order, backend)
pauli_to_comp_basis(nqubits, normalize, sparse, order, pauli_order, backend)

0 comments on commit 403621b

Please sign in to comment.