Skip to content

state

Quantum state vectors.

State

Represent a quantum state vector.

Source code in shadowsim/core/state.py
class State:
    """Represent a quantum state vector."""

    def __init__(self, state: npt.NDArray[np.complexfloating], num_qubits: int, local_dim: int = 2) -> None:
        """Initialize a State object which represents a quantum state.

        Parameter state: The state vector to initialize the state with.
        Precondition: state is a numpy array.

        Parameter num_qubits: The number of qubits the state is defined on.
        Precondition: num_qubits is a non-negative integer.

        Parameter local_dim: The local dimension of the state.
        Precondition: local_dim is a positive integer.
        """
        assert num_qubits >= 0, "num_qubits must be non-negative"
        assert isinstance(num_qubits, int), "num_qubits must be an integer"
        assert local_dim > 0, "local_dim must be a positive integer"
        assert isinstance(local_dim, int), "local_dim must be an integer"
        self._num_qubits = num_qubits
        self._local_dim = local_dim
        self.state = _as_valid_state_vector(state, self._num_qubits, self._local_dim)

    def get_state(self) -> npt.NDArray[np.complexfloating]:
        """Return the state vector as a numpy array."""
        return self.state

    def set_state(self, state: npt.NDArray[np.complexfloating]) -> None:
        """Set the state vector to a new numpy array."""
        self.state = _as_valid_state_vector(state, self._num_qubits, self._local_dim)

    def get_num_qubits(self) -> int:
        """Return the number of qubits the state is defined on."""
        return self._num_qubits

    def get_local_dim(self) -> int:
        """Return the local dimension of the state."""
        return self._local_dim

    def to_numpy(self) -> npt.NDArray[np.complexfloating]:
        """Return the state vector as a numpy array."""
        return self.state

    def __str__(self) -> str:
        """Return a string representation of the state."""
        return f"State(num_qubits={self._num_qubits}, local_dim={self._local_dim}, shape={self.state.shape})"

    def __repr__(self) -> str:
        """Return a string representation of the state."""
        return f"State(state={self.state!r}, num_qubits={self._num_qubits}, local_dim={self._local_dim})"

__init__

__init__(
    state: NDArray[complexfloating],
    num_qubits: int,
    local_dim: int = 2,
) -> None

Initialize a State object which represents a quantum state.

Parameter state: The state vector to initialize the state with. Precondition: state is a numpy array.

Parameter num_qubits: The number of qubits the state is defined on. Precondition: num_qubits is a non-negative integer.

Parameter local_dim: The local dimension of the state. Precondition: local_dim is a positive integer.

Source code in shadowsim/core/state.py
def __init__(self, state: npt.NDArray[np.complexfloating], num_qubits: int, local_dim: int = 2) -> None:
    """Initialize a State object which represents a quantum state.

    Parameter state: The state vector to initialize the state with.
    Precondition: state is a numpy array.

    Parameter num_qubits: The number of qubits the state is defined on.
    Precondition: num_qubits is a non-negative integer.

    Parameter local_dim: The local dimension of the state.
    Precondition: local_dim is a positive integer.
    """
    assert num_qubits >= 0, "num_qubits must be non-negative"
    assert isinstance(num_qubits, int), "num_qubits must be an integer"
    assert local_dim > 0, "local_dim must be a positive integer"
    assert isinstance(local_dim, int), "local_dim must be an integer"
    self._num_qubits = num_qubits
    self._local_dim = local_dim
    self.state = _as_valid_state_vector(state, self._num_qubits, self._local_dim)

get_state

get_state() -> NDArray[complexfloating]

Return the state vector as a numpy array.

Source code in shadowsim/core/state.py
def get_state(self) -> npt.NDArray[np.complexfloating]:
    """Return the state vector as a numpy array."""
    return self.state

set_state

set_state(state: NDArray[complexfloating]) -> None

Set the state vector to a new numpy array.

Source code in shadowsim/core/state.py
def set_state(self, state: npt.NDArray[np.complexfloating]) -> None:
    """Set the state vector to a new numpy array."""
    self.state = _as_valid_state_vector(state, self._num_qubits, self._local_dim)

get_num_qubits

get_num_qubits() -> int

Return the number of qubits the state is defined on.

Source code in shadowsim/core/state.py
def get_num_qubits(self) -> int:
    """Return the number of qubits the state is defined on."""
    return self._num_qubits

get_local_dim

get_local_dim() -> int

Return the local dimension of the state.

Source code in shadowsim/core/state.py
def get_local_dim(self) -> int:
    """Return the local dimension of the state."""
    return self._local_dim

to_numpy

to_numpy() -> NDArray[complexfloating]

Return the state vector as a numpy array.

Source code in shadowsim/core/state.py
def to_numpy(self) -> npt.NDArray[np.complexfloating]:
    """Return the state vector as a numpy array."""
    return self.state

__str__

__str__() -> str

Return a string representation of the state.

Source code in shadowsim/core/state.py
def __str__(self) -> str:
    """Return a string representation of the state."""
    return f"State(num_qubits={self._num_qubits}, local_dim={self._local_dim}, shape={self.state.shape})"

__repr__

__repr__() -> str

Return a string representation of the state.

Source code in shadowsim/core/state.py
def __repr__(self) -> str:
    """Return a string representation of the state."""
    return f"State(state={self.state!r}, num_qubits={self._num_qubits}, local_dim={self._local_dim})"