Skip to content

StateVector ​

A pure quantum state represented as a complex column vector of dimension 2n, where n is the number of qubits. Each element corresponds to the probability amplitude of a computational-basis state.

The state vector |ψ⟩ must satisfy the normalization condition:

⟨ψ|ψ⟩=∑i|ψi|2=1

Constructors ​

Default constructor ​

python
StateVector()

Constructs a state vector for a single-qubit system in the |0⟩ state: [1+0j,0+0j].

Copy constructor ​

python
StateVector(other: StateVector)

Construct a StateVector as a copy of another StateVector.

ParameterTypeDescription
otherStateVectorAn existing StateVector to copy.

From a dictionary ​

python
StateVector(data: dict[int, complex])

Construct a state vector from a dictionary mapping basis-state indices to complex amplitudes. Indices not present in the dictionary are set to zero. The vector is automatically normalized to unit length.

ParameterTypeDescription
datadict[int, complex]Mapping from basis-state index to amplitude.

From a complex array ​

python
StateVector(data: list[complex])

Construct a state vector from a flat list (or numpy array) of complex amplitudes. The vector is automatically normalized to unit length.

ParameterTypeDescription
datalist[complex]A 1-D array of complex amplitudes.

From a numpy array ​

python
StateVector(data: numpy.ndarray)

Construct a state vector from a 1-D complex numpy array (or Eigen vector). The vector is automatically normalized to unit length.

ParameterTypeDescription
datanumpy.ndarrayA 1-D array of complex amplitudes.

From qubit count ​

python
StateVector(qbit_total: int)

Construct a state vector for qbit_total qubits initialized to the all-zeros state |0⟩⊗n.

ParameterTypeDescription
qbit_totalintThe number of qubits. The resulting vector has dimension 2qbit_total.

Methods ​

ndarray ​

python
ndarray() -> numpy.ndarray[numpy.complex128]

Return the internal state-vector data as a 1-D numpy.ndarray.

purity ​

python
purity() -> complex

Return the purity Tr(ρ2) of the state. For a pure state represented by a state vector, the purity is always 1+0j.

evolve ​

python
evolve(circuit: QCircuit) -> StateVector

Evolve the state vector under the given quantum circuit. Returns a new StateVector with the result; the original object is not modified.

ParameterTypeDescription
circuitQCircuitThe quantum circuit to apply.

update_by_evolve ​

python
update_by_evolve(circuit: QCircuit) -> StateVector

Evolve the state vector under the given quantum circuit in place, updating the internal data. Also returns the resulting StateVector.

ParameterTypeDescription
circuitQCircuitThe quantum circuit to apply.

is_valid ​

python
is_valid() -> bool

Check whether the internal data constitutes a valid quantum state vector (normalized to unit length).

get_density_matrix ​

python
get_density_matrix() -> Matrix

Compute the density matrix ρ=|ψ⟩⟨ψ| corresponding to this state vector.

at ​

python
at(idx: int) -> complex

Return the amplitude at the specified basis-state index.

ParameterTypeDescription
idxintThe index of the basis state (0-based).

dim ​

python
dim() -> int

Return the dimension of the state vector, i.e., 2n for n qubits.

Operators ​

Equality ​

python
sv_a == sv_b -> bool

Return True if the two StateVector objects have identical internal data.

Examples ​

python
import numpy as np
from pyqpanda3.quantum_info import StateVector

# Construct from a list (Bell state |00> + |11>)
sv = StateVector([1/np.sqrt(2), 0, 0, 1/np.sqrt(2)])

print(sv.dim())       # 4
print(sv.is_valid())  # True
print(sv.purity())    # (1.0+0j)

# Access an amplitude
amp = sv.at(3)  # (0.7071...+0j)
print("amp:", amp)

# Convert to density matrix
dm = sv.get_density_matrix()

# Get numpy array
arr = sv.ndarray()

Running the example produces:

4
True
(1.0000000000000004+0j)
amp: (0.7071067811865476+0j)

See Also ​

Released under the MIT License.