Main Content

probability

Probability of measuring qubits in given state

Since R2023a

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

Description

p = probability(s,qubits) returns the probability of measuring all specified qubits in the |1 state for the input quantum state or measurement s. The input s must be a QuantumState or QuantumMeasurement object.

  • If s is a QuantumState object, then probability returns the probability of making the specified measurement based on that quantum state.

  • If s is a QuantumMeasurement object, then probability returns the estimated probability of the specified measurement based on that measurement result.

example

p = probability(s,qubits,state) returns the probability of measuring qubits in the specified state.

example

Examples

collapse all

Create a quantum circuit that consists of a controlled X gate that acts on a control qubit with index 1 and a target qubit with index 2.

g = cxGate(1,2);
c = quantumCircuit(g);

Simulate the circuit using an initial state of the qubits in the X basis of |+.

s = simulate(c,"+-")
s = 

  QuantumState with properties:

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

Show the final state of the circuit in the X basis and the Z basis.

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

    "1 * |-->"
str = formula(s,Basis="Z")
str = 

    "0.5  * |00> +
     -0.5 * |01> +
     -0.5 * |10> +
     0.5  * |11>"

Find the probability of measuring qubit 2 in the |1 state after running the circuit. Because the final state consists of the states -0.5*|01> and 0.5*|11>, the probability function returns -0.5^2 + 0.5^2 = 0.5.

p = probability(s,2)
p =

    0.5000

Find the probability of measuring qubit 1 in the | state.

p = probability(s,1,"-")
p =

    1.0000

Find the probability of measuring qubits 1 and 2 in the |10 state simultaneously.

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

    0.2500

Create a controlled X gate that acts on a control qubit with index 1 and a target qubit with index 2. By default, when you create a quantum circuit from this gate, the initial state of the qubits is |00. To prepare the initial state of the qubits as |+ instead, apply the X gate to qubit 2 and apply the Hadamard gates to qubits 1 and 2 before applying the controlled X gate.

Create a quantum circuit that consists of these gates.

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

Run the circuit on a remote device. Note that your remote service account will be charged when you run the circuit.

dev = quantum.backend.QuantumDeviceAWS("Lucy");
task = run(c,dev);

Wait for the running task to finish, and fetch the measurement result of running the circuit.

wait(task);
m = fetchOutput(task)
m = 

  QuantumMeasurement with properties:

    MeasuredStates: [4×1 string]
            Counts: [4×1 double]
     Probabilities: [4x1 double]
         NumQubits: 2

Show the measurement result of running the circuit.

table(m.Counts,m.Probabilities,m.MeasuredStates, ...
    VariableNames=["Counts","Probabilities","States"])
ans =

  4×3 table

    Counts    Probabilities    States
    ______    _____________    ______

      31          0.31         "00" 
      25          0.25         "01" 
      20           0.2         "10" 
      24          0.24         "11" 

Find the probability of measuring qubit 2 in the |1 state after running the circuit. Because the final state consists of the "01" and "11", the probability function returns 0.25 + 0.24 = 0.49.

p = probability(m,2)
p =

    0.4900

Find the probability of measuring qubits 1 and 2 in the |10 state simultaneously.

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

    0.2000

Input Arguments

collapse all

Quantum state or measurement, specified as a QuantumState object or a QuantumMeasurement object.

Qubit indices, specified as a vector of positive integers.

State for which qubit measurement probability is calculated, specified as a string scalar.

If s is a QuantumMeasurement object, then state can be "0" or "1", meaning all qubits are being measured in the |0 or |1 state. The state string can also have length(qubits) characters of "0" and "1" to specify the measured state of each qubit. If s is a QuantumState object, then state can additionally contain "+" and "-". The default state is "1".

Version History

Introduced in R2023a