qiskit.quantum_info.Statevector¶

class
Statevector
(data, dims=None)[소스]¶ Statevector class
Initialize a statevector object.
 매개변수
data (vector_like) – a complex statevector.
dims (int or tuple or list) – Optional. The subsystem dimension of the state (See additional information).
 예외
QiskitError – if input data is not valid.
 Additional Information:
The
dims
kwarg can be None, an integer, or an iterable of integers.Iterable
– the subsystem dimensions are the values in the list with the total number of subsystems given by the length of the list.Int
orNone
– the length of the input vector specifies the total dimension of the density matrix. If it is a power of two the state will be initialized as an Nqubit state. If it is not a power of two the state will have a single ddimensional subsystem.

__init__
(data, dims=None)[소스]¶ Initialize a statevector object.
 매개변수
data (vector_like) – a complex statevector.
dims (int or tuple or list) – Optional. The subsystem dimension of the state (See additional information).
 예외
QiskitError – if input data is not valid.
 Additional Information:
The
dims
kwarg can be None, an integer, or an iterable of integers.Iterable
– the subsystem dimensions are the values in the list with the total number of subsystems given by the length of the list.Int
orNone
– the length of the input vector specifies the total dimension of the density matrix. If it is a power of two the state will be initialized as an Nqubit state. If it is not a power of two the state will have a single ddimensional subsystem.
Methods
__init__
(data[, dims])Initialize a statevector object.
add
(other)Return the linear combination self + other.
Return the conjugate of the operator.
copy
()Make a copy of current operator.
dims
([qargs])Return tuple of input dimension for specified subsystems.
equiv
(other[, rtol, atol])Return True if statevectors are equivalent up to global phase.
evolve
(other[, qargs])Evolve a quantum state by the operator.
expand
(other)Return the tensor product state other ⊗ self.
expectation_value
(oper[, qargs])Compute the expectation value of an operator.
from_instruction
(instruction)Return the output statevector of an instruction.
from_int
(i, dims)Return a computational basis statevector.
from_label
(label)Return a tensor product of Pauli X,Y,Z eigenstates.
is_valid
([atol, rtol])Return True if a Statevector has norm 1.
measure
([qargs])Measure subsystems and return outcome and postmeasure state.
multiply
(other)Return the scalar multipled state other * self.
probabilities
([qargs, decimals])Return the subsystem measurement probability vector.
probabilities_dict
([qargs, decimals])Return the subsystem measurement probability dictionary.
purity
()Return the purity of the quantum state.
reset
([qargs])Reset state or subsystems to the 0state.
sample_counts
(shots[, qargs])Sample a dict of qubit measurement outcomes in the computational basis.
sample_memory
(shots[, qargs])Sample a list of qubit measurement outcomes in the computational basis.
seed
([value])Set the seed for the quantum state RNG.
set_atol
(value)Set the class default absolute tolerance parameter for float comparisons.
set_rtol
(value)Set the class default relative tolerance parameter for float comparisons.
subtract
(other)Return the linear operator self  other.
tensor
(other)Return the tensor product state self ⊗ other.
Returns the statevector as a counts dict of probabilities.
to_dict
([decimals])Convert the statevector to dictionary form.
Convert state to a rank1 projector operator
trace
()Return the trace of the quantum state as a density matrix.
Attributes
The absolute tolerance parameter for float comparisons.
Return data.
Return total state dimension.
Return the number of qubits if a Nqubit state or None otherwise.
The relative tolerance parameter for float comparisons.

add
(other)¶ Return the linear combination self + other.
DEPRECATED: use
state + other
instead. 매개변수
other (QuantumState) – a quantum state object.
 반환값
the linear combination self + other.
 반환 형식
LinearOperator
 예외
QiskitError – if other is not a quantum state, or has incompatible dimensions.

property
atol
¶ The absolute tolerance parameter for float comparisons.

copy
()¶ Make a copy of current operator.

property
data
¶ Return data.

property
dim
¶ Return total state dimension.

dims
(qargs=None)¶ Return tuple of input dimension for specified subsystems.

equiv
(other, rtol=None, atol=None)[소스]¶ Return True if statevectors are equivalent up to global phase.
 매개변수
other (Statevector) – a statevector object.
rtol (float) – relative tolerance value for comparison.
atol (float) – absolute tolerance value for comparison.
 반환값
True if statevectors are equivalent up to global phase.
 반환 형식
bool

evolve
(other, qargs=None)[소스]¶ Evolve a quantum state by the operator.
 매개변수
other (Operator) – The operator to evolve by.
qargs (list) – a list of Statevector subsystem positions to apply the operator on.
 반환값
the output quantum state.
 반환 형식
 예외
QiskitError – if the operator dimension does not match the specified Statevector subsystem dimensions.

expand
(other)[소스]¶ Return the tensor product state other ⊗ self.
 매개변수
other (Statevector) – a quantum state object.
 반환값
the tensor product state other ⊗ self.
 반환 형식
 예외
QiskitError – if other is not a quantum state.

expectation_value
(oper, qargs=None)[소스]¶ Compute the expectation value of an operator.
 매개변수
oper (Operator) – an operator to evaluate expval of.
qargs (None or list) – subsystems to apply operator on.
 반환값
the expectation value.
 반환 형식
complex

classmethod
from_instruction
(instruction)[소스]¶ Return the output statevector of an instruction.
The statevector is initialized in the state \({0,\ldots,0}\rangle\) of the same number of qubits as the input instruction or circuit, evolved by the input instruction, and the output statevector returned.
 매개변수
instruction (qiskit.circuit.Instruction or QuantumCircuit) – instruction or circuit
 반환값
The final statevector.
 반환 형식
 예외
QiskitError – if the instruction contains invalid instructions for the statevector simulation.

static
from_int
(i, dims)[소스]¶ Return a computational basis statevector.
 매개변수
i (int) – the basis state element.
dims (int or tuple or list) – The subsystem dimensions of the statevector (See additional information).
 반환값
The computational basis state \(i\rangle\).
 반환 형식
 Additional Information:
The
dims
kwarg can be an integer or an iterable of integers.Iterable
– the subsystem dimensions are the values in the list with the total number of subsystems given by the length of the list.Int
– the integer specifies the total dimension of the state. If it is a power of two the state will be initialized as an Nqubit state. If it is not a power of two the state will have a single ddimensional subsystem.

classmethod
from_label
(label)[소스]¶ Return a tensor product of Pauli X,Y,Z eigenstates.
¶ Label
Statevector
"0"
\([1, 0]\)
"1"
\([0, 1]\)
"+"
\([1 / \sqrt{2}, 1 / \sqrt{2}]\)
""
\([1 / \sqrt{2}, 1 / \sqrt{2}]\)
"r"
\([1 / \sqrt{2}, i / \sqrt{2}]\)
"l"
\([1 / \sqrt{2}, i / \sqrt{2}]\)
 매개변수
label (string) – a eigenstate string ket label (see table for allowed values).
 반환값
The Nqubit basis state density matrix.
 반환 형식
 예외
QiskitError – if the label contains invalid characters, or the length of the label is larger than an explicitly specified num_qubits.

measure
(qargs=None)¶ Measure subsystems and return outcome and postmeasure state.
Note that this function uses the QuantumStates internal random number generator for sampling the measurement outcome. The RNG seed can be set using the
seed()
method. 매개변수
qargs (list or None) – subsystems to sample measurements for, if None sample measurement of all subsystems (Default: None).
 반환값
 the pair
(outcome, state)
whereoutcome
is the measurement outcome string label, and
state
is the collapsed postmeasurement state for the corresponding outcome.
 the pair
 반환 형식
tuple

multiply
(other)¶ Return the scalar multipled state other * self.
 매개변수
other (complex) – a complex number.
 반환값
the scalar multipled state other * self.
 반환 형식
QuantumState
 예외
QiskitError – if other is not a valid complex number.

property
num_qubits
¶ Return the number of qubits if a Nqubit state or None otherwise.

probabilities
(qargs=None, decimals=None)[소스]¶ Return the subsystem measurement probability vector.
Measurement probabilities are with respect to measurement in the computation (diagonal) basis.
 매개변수
qargs (None or list) – subsystems to return probabilities for, if None return for all subsystems (Default: None).
decimals (None or int) – the number of decimal places to round values. If None no rounding is done (Default: None).
 반환값
The Numpy vector array of probabilities.
 반환 형식
np.array
예제
Consider a 2qubit product state \(\psi\rangle=+\rangle\otimes0\rangle\).
from qiskit.quantum_info import Statevector psi = Statevector.from_label('+0') # Probabilities for measuring both qubits probs = psi.probabilities() print('probs: {}'.format(probs)) # Probabilities for measuring only qubit0 probs_qubit_0 = psi.probabilities([0]) print('Qubit0 probs: {}'.format(probs_qubit_0)) # Probabilities for measuring only qubit1 probs_qubit_1 = psi.probabilities([1]) print('Qubit1 probs: {}'.format(probs_qubit_1))
probs: [0.5 0. 0.5 0. ] Qubit0 probs: [1. 0.] Qubit1 probs: [0.5 0.5]
We can also permute the order of qubits in the
qargs
list to change the qubit position in the probabilities outputfrom qiskit.quantum_info import Statevector psi = Statevector.from_label('+0') # Probabilities for measuring both qubits probs = psi.probabilities([0, 1]) print('probs: {}'.format(probs)) # Probabilities for measuring both qubits # but swapping qubits 0 and 1 in output probs_swapped = psi.probabilities([1, 0]) print('Swapped probs: {}'.format(probs_swapped))
probs: [0.5 0. 0.5 0. ] Swapped probs: [0.5 0.5 0. 0. ]

probabilities_dict
(qargs=None, decimals=None)¶ Return the subsystem measurement probability dictionary.
Measurement probabilities are with respect to measurement in the computation (diagonal) basis.
This dictionary representation uses a Ketlike notation where the dictionary keys are qudit strings for the subsystem basis vectors. If any subsystem has a dimension greater than 10 comma delimiters are inserted between integers so that subsystems can be distinguished.
 매개변수
qargs (None or list) – subsystems to return probabilities for, if None return for all subsystems (Default: None).
decimals (None or int) – the number of decimal places to round values. If None no rounding is done (Default: None).
 반환값
The measurement probabilities in dict (ket) form.
 반환 형식
dict

reset
(qargs=None)[소스]¶ Reset state or subsystems to the 0state.
 매개변수
qargs (list or None) – subsystems to reset, if None all subsystems will be reset to their 0state (Default: None).
 반환값
the reset state.
 반환 형식
 Additional Information:
If all subsystems are reset this will return the ground state on all subsystems. If only a some subsystems are reset this function will perform a measurement on those subsystems and evolve the subsystems so that the collapsed postmeasurement states are rotated to the 0state. The RNG seed for this sampling can be set using the
seed()
method.

property
rtol
¶ The relative tolerance parameter for float comparisons.

sample_counts
(shots, qargs=None)¶ Sample a dict of qubit measurement outcomes in the computational basis.
 매개변수
shots (int) – number of samples to generate.
qargs (None or list) – subsystems to sample measurements for, if None sample measurement of all subsystems (Default: None).
 반환값
sampled counts dictionary.
 반환 형식
Additional Information:
This function samples measurement outcomes using the measure
probabilities()
for the current state and qargs. It does not actually implement the measurement so the current state is not modified.The seed for random number generator used for sampling can be set to a fixed value by using the stats
seed()
method.

sample_memory
(shots, qargs=None)¶ Sample a list of qubit measurement outcomes in the computational basis.
 매개변수
shots (int) – number of samples to generate.
qargs (None or list) – subsystems to sample measurements for, if None sample measurement of all subsystems (Default: None).
 반환값
list of sampled counts if the order sampled.
 반환 형식
np.array
Additional Information:
This function samples measurement outcomes using the measure
probabilities()
for the current state and qargs. It does not actually implement the measurement so the current state is not modified.The seed for random number generator used for sampling can be set to a fixed value by using the stats
seed()
method.

seed
(value=None)¶ Set the seed for the quantum state RNG.

classmethod
set_atol
(value)¶ Set the class default absolute tolerance parameter for float comparisons.
DEPRECATED: use operator.atol = value instead

classmethod
set_rtol
(value)¶ Set the class default relative tolerance parameter for float comparisons.
DEPRECATED: use operator.rtol = value instead

subtract
(other)¶ Return the linear operator self  other.
DEPRECATED: use
state  other
instead. 매개변수
other (QuantumState) – a quantum state object.
 반환값
the linear combination self  other.
 반환 형식
LinearOperator
 예외
QiskitError – if other is not a quantum state, or has incompatible dimensions.

tensor
(other)[소스]¶ Return the tensor product state self ⊗ other.
 매개변수
other (Statevector) – a quantum state object.
 반환값
the tensor product operator self ⊗ other.
 반환 형식
 예외
QiskitError – if other is not a quantum state.

to_counts
()[소스]¶ Returns the statevector as a counts dict of probabilities.
DEPRECATED: use
probabilities_dict()
instead. 반환값
Counts of probabilities.
 반환 형식
dict

to_dict
(decimals=None)[소스]¶ Convert the statevector to dictionary form.
This dictionary representation uses a Ketlike notation where the dictionary keys are qudit strings for the subsystem basis vectors. If any subsystem has a dimension greater than 10 comma delimiters are inserted between integers so that subsystems can be distinguished.
 매개변수
decimals (None or int) – the number of decimal places to round values. If None no rounding is done (Default: None).
 반환값
the dictionary form of the Statevector.
 반환 형식
dict
예제
The ketform of a 2qubit statevector \(\psi\rangle = \rangle\otimes 0\rangle\)
from qiskit.quantum_info import Statevector psi = Statevector.from_label('0') print(psi.to_dict())
{'00': (0.7071067811865475+0j), '10': (0.7071067811865475+0j)}
For nonqubit subsystems the integer range can go from 0 to 9. For example in a qutrit system
import numpy as np from qiskit.quantum_info import Statevector vec = np.zeros(9) vec[0] = 1 / np.sqrt(2) vec[1] = 1 / np.sqrt(2) psi = Statevector(vec, dims=(3, 3)) print(psi.to_dict())
{'00': (0.7071067811865475+0j), '22': (0.7071067811865475+0j)}
For large subsystem dimensions delimeters are required. The following example is for a 20dimensional system consisting of a qubit and 10dimensional qudit.
import numpy as np from qiskit.quantum_info import Statevector vec = np.zeros(2 * 10) vec[0] = 1 / np.sqrt(2) vec[1] = 1 / np.sqrt(2) psi = Statevector(vec, dims=(2, 10)) print(psi.to_dict())
{'00': (0.7071067811865475+0j), '91': (0.7071067811865475+0j)}