Main Content

randsample

Random sample of quantum state

Since R2023a

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

Description

example

m = randsample(state,numShots) randomly samples the input quantum state with a specified number of shots and returns the resulting simulated measurement as a QuantumMeasurement object.

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.

state = simulate(c)
state = 

  QuantumState with properties:

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

Show the final state of the circuit.

formula(state)
ans = 

    "0.70711 * |00> +
     0.70711 * |11>"

Randomly sample the final state with 100 shots to simulate the measurement of the quantum state after running the circuit.

m = randsample(state,100)
m = 

  QuantumMeasurement with properties:

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

Show the counts and estimated probabilities of the measured states.

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

  2×3 table

    Counts    Probabilities    States
    ______    _____________    ______

      56          0.56          "00" 
      44          0.44          "11" 

Input Arguments

collapse all

Quantum state, specified as a QuantumState object.

Number of simulated shots, specified as a positive integer scalar.

Tips

  • The randsample function uses the current random number generator, the same generator that underlies rand, randi, and randn. Therefore, each call to randsample returns a different result. You can control that shared random number generator using rng.

  • On quantum processing unit (QPU) devices to date, real measurements can be more noisy than measurements resulting from randomly sampling a QuantumState object. For more information, see Run Circuit and Make Measurement.

Version History

Introduced in R2023a