Main Content

quantum.gate.QuantumState Class

Namespace: quantum.gate

State of qubits in quantum circuit

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

Description

A QuantumState object represents the quantum state of the qubits in a quantum circuit. This object contains information about the normalized amplitudes of all possible 2n basis states of the n qubits that form the quantum state.

Creation

Use the simulate function to simulate a quantum circuit and return the final state of the circuit as a QuantumState object. Additionally, you can construct a QuantumState object directly using one of the following syntaxes.

Description

s = quantum.gate.QuantumState(basisString) translates a string containing the letters "0", "1", "+", or "-" into a quantum state. Each letter represents the basis state that one qubit is in, where the first letter maps to the first qubit and so on.

example

s = quantum.gate.QuantumState(coeffs) translates a complex vector of coefficients into a quantum state with normalized probability amplitudes.

The coeffs vector must have length 2n, where n is the number of qubits. The 2n elements of coeffs represent the unnormalized amplitudes for all the possible 2n basis states of the qubits in the Z basis. The Amplitudes property of the returned quantum state is set to coeffs/norm(coeffs).

example

Input Arguments

expand all

Basis states that qubits are in, specified as a string scalar containing the letters "0", "1", "+", or "-".

Coefficients for basis states, specified as a complex vector of length 2n, where n is the number of qubits.

Data Types: double | single
Complex Number Support: Yes

Properties

expand all

Basis states in the Z basis, specified as a string array.

The ordering used for the basis states follows most textbooks. For example, for a circuit with two qubits, all possible basis states are |q1q2=[|00,|01,|10,|11]T. The BasisStates property returns these possible basis states as the string array ["00"; "01"; "10"; "11"]. For more information, see Quantum Circuit.

Attributes:

GetAccess
public
SetAccess
public

Amplitudes of the basis states, returned as a complex vector. These amplitudes are normalized, where the sum of the probabilities of the basis states (magnitude squared of the amplitudes) is 1.

Attributes:

GetAccess
public
SetAccess
private

Number of qubits, returned as a positive integer scalar.

Attributes:

GetAccess
public
SetAccess
private

Methods

expand all

Examples

collapse all

Create a quantum circuit that consists of a Hadamard gate and a controlled X gate to entangle two qubits.

gates = [hGate(1); cxGate(1,2)];
c = quantumCircuit(gates);

Simulate the circuit using the default initial state, where each qubit is in the |0 state.

s = simulate(c)
s = 

  QuantumState with properties:

    BasisStates: [4×1 string]
     Amplitudes: [4×1 double]
      NumQubits: 2

Show the basis states and their amplitudes for the final state of the circuit.

states = s.BasisStates
states = 

  4×1 string array

    "00"
    "01"
    "10"
    "11"
amps = s.Amplitudes
amps =

    0.7071
         0
         0
    0.7071

Simulate a local measurement of this state by randomly sampling with 100 shots. Show the measured states and their counts.

m = randsample(s,100);
states = m.MeasuredStates
states = 

  2×1 string array

    "00"
    "11"

counts = m.Counts
counts =

    54
    46

Construct a quantum state from a string specifying the basis state that each qubit is in. For example, create the state |++1 for four qubits.

s = quantum.gate.QuantumState("+-+1")
s = 

  QuantumState with properties:

    BasisStates: [16×1 string]
     Amplitudes: [16×1 double]
      NumQubits: 4

Show this state as a formula.

str = formula(s)
str = 

    "1 * |+-+1>"

Show this state as a formula in the Z basis.

str = formula(s,Basis="Z")
str = 

    "0.35355  * |0001> +
     0.35355  * |0011> +
     -0.35355 * |0101> +
     -0.35355 * |0111> +
     0.35355  * |1001> +
     0.35355  * |1011> +
     -0.35355 * |1101> +
     -0.35355 * |1111>"

Show this state as a formula in the X basis.

str = formula(s,Basis="X")
str = 

    "0.70711  * |+-++> +
     -0.70711 * |+-+->"

From the quantum state s, you can plot the histogram of the possible states and their probabilities by using histogram (for example, use the Z basis).

histogram(s,Basis="Z")

Histogram of eight possible states and their probabilities

You can also query the possible states and their probabilities by using querystates (for example, use the X basis).

[states,probabilities] = querystates(s,Basis="X")
states = 

  2×1 string array

    "+-++"
    "+-+-"


probabilities =

    0.5000
    0.5000

Construct a quantum state for two qubits. Specify the amplitude coefficients for the four possible basis states of the qubits as a complex vector.

s = quantum.gate.QuantumState([1; -1; 1i; sqrt(2)*1i])
s = 

  QuantumState with properties:

    BasisStates: [4×1 string]
     Amplitudes: [4×1 double]
      NumQubits: 2

Show this state as a formula.

str = formula(s)
str = 

    "(0.44721+0i)  * |00> +
     (-0.44721+0i) * |01> +
     (0+0.44721i)  * |10> +
     (0+0.63246i)  * |11>"

Find the probability of measuring the two qubits in the |10 state simultaneously.

p = probability(s,[1 2],"10")
p =

    0.2000

Find the probability of measuring the two qubits in the |11 state simultaneously.

p = probability(s,[1 2],"11")
p =

    0.4000

Find the probability of measuring the first qubit in the |1 state. The probability function returns the sum 0.2 + 0.4 = 0.6.

p = probability(s,1)
p =

    0.6000

Tips

  • A quantum state can contain information that is not observable. Only properties of the quantum state that translate into probabilities (magnitude squared of the state amplitudes) are measurable. For example, multiplying every amplitude of a quantum state by –1 or exp(iθ) has no measurable impact. This type of transformation is called applying a global phase. However, multiplying one of the amplitudes by –1 or exp(iθ) (applying a relative phase) has a measurable impact. If you measure the state directly after applying the relative phase, you do not see a difference in the probabilities. But if you apply additional gate operations on the qubits, you can detect the difference due to the relative phase.

  • To return measurement probabilities of possible states, you can use any of these methods: histogram, probability, and querystates. You can also use randsample with a specific number of shots to simulate real measurements on physical quantum devices.

Version History

Introduced in R2023a