Skip to content

Commit

Permalink
put density matrix into states
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles MOUSSA committed Dec 31, 2024
1 parent 7c154d0 commit 13d477e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
21 changes: 21 additions & 0 deletions qadence/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import torch
from numpy.typing import ArrayLike
from pyqtorch.utils import DensityMatrix
from torch import Tensor, concat
from torch.distributions import Categorical, Distribution

Expand Down Expand Up @@ -37,6 +38,8 @@
"is_normalized",
"rand_bitstring",
"equivalent_state",
"DensityMatrix",
"density_mat",
]

ATOL_64 = 1e-14 # 64 bit precision
Expand Down Expand Up @@ -319,6 +322,24 @@ def random_state(
return state


# DENSITY MATRIX


def density_mat(state: Tensor) -> DensityMatrix:
"""
Computes the density matrix from a pure state vector.
Arguments:
state: The pure state vector :math:`|\\psi\\rangle`.
Returns:
Tensor: The density matrix :math:`\\rho = |\psi \\rangle \\langle\\psi|`.
"""
if isinstance(state, DensityMatrix):
return state
return DensityMatrix(torch.einsum("bi,bj->bij", (state, state.conj())))


# BLOCKS


Expand Down
18 changes: 1 addition & 17 deletions qadence/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import numpy as np
import sympy
from numpy.typing import ArrayLike
from pyqtorch.utils import DensityMatrix
from torch import Tensor, einsum, stack, vmap
from torch import Tensor, stack, vmap
from torch import complex as make_complex
from torch.linalg import eigvals

Expand Down Expand Up @@ -293,18 +292,3 @@ def one_qubit_projector_matrix(state: str) -> Tensor:
P1 = partial(one_qubit_projector, "1")
P0_MATRIX = one_qubit_projector_matrix("0")
P1_MATRIX = one_qubit_projector_matrix("1")


def density_mat(state: Tensor) -> DensityMatrix:
"""
Computes the density matrix from a pure state vector.
Arguments:
state: The pure state vector :math:`|\\psi\\rangle`.
Returns:
Tensor: The density matrix :math:`\\rho = |\psi \\rangle \\langle\\psi|`.
"""
if isinstance(state, DensityMatrix):
return state
return DensityMatrix(einsum("bi,bj->bij", (state, state.conj())))
4 changes: 2 additions & 2 deletions tests/backends/pyq/test_quantum_pyq.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
Z,
)
from qadence.parameters import FeatureParameter, Parameter
from qadence.states import random_state, uniform_state, zero_state
from qadence.states import DensityMatrix, density_mat, random_state, uniform_state, zero_state
from qadence.transpile import set_trainable
from qadence.types import PI, BackendName, DiffMode
from qadence.utils import P0, P1, DensityMatrix, density_mat
from qadence.utils import P0, P1


def custom_obs() -> AbstractBlock:
Expand Down

0 comments on commit 13d477e

Please sign in to comment.