Random sample of quantum state
Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.
Simulate Measurement of Quantum Circuit by Random Sampling
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 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.
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"
state — Quantum state
Quantum state, specified as a
numShots — Number of simulated shots
positive integer scalar
Number of simulated shots, specified as a positive integer scalar.
randsamplefunction uses the current random number generator, the same generator that underlies
randn. Therefore, each call to
randsamplereturns a different result. You can control that shared random number generator using
On quantum processing unit (QPU) devices to date, real measurements can be more noisy than measurements resulting from randomly sampling a
QuantumStateobject. For more information, see Run Circuit and Make Measurement.
Introduced in R2023a