Simulate quantum circuit
Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.
The default value of
"0...0" with as
"0" characters as there are qubits in
each qubit is in the state. You can specify
inputState as either a string
"-" with as many characters as there are qubits in
Simulate Quantum Circuit
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.
s = simulate(c)
s = 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>"
Plot the histogram of probabilities to measure possible states from the final state of the circuit.
Simulate Quantum Circuit with Different Initial States
Create a quantum circuit that consists of a controlled X gate.
c = quantumCircuit(cxGate(1,2));
Simulate the circuit using initial states of
the final state of the circuit for each initial state after running the circuit.
for ket=["00", "01", "10", "11"] s = simulate(c,ket); disp("|" + ket + "> -> " + formula(s)); end
|00> -> 1 * |00> |01> -> 1 * |01> |10> -> 1 * |11> |11> -> 1 * |10>
c — Quantum circuit
Quantum circuit, specified as a
inputState — Initial state of quantum circuit
string scalar |
Initial state of quantum circuit, specified as a string scalar or a
The default value is
"0...0"with as many characters
"0"as there are qubits in
You can specify a string containing
"-"with as many characters as there are qubits in
c. For example, to specify four qubits that are in the initial state , use
You can specify a
QuantumStateobject. For example, you can construct the initial state using
inputState = quantum.gate.QuantumState("0+1-"). The
quantum.gate.QuantumStateconstructor also accepts a complex vector to create a quantum state. For example, you can construct the initial state using
inputState = quantum.gate.QuantumState([0 1i/sqrt(2) 1/sqrt(2) 0]). You can also specify a
QuantumStateobject that is returned by running
simulateon another quantum circuit (with the same number of qubits).
s — Final state after running the circuit
Final state after running the circuit, returned as a
object. This object contains the normalized amplitudes of all possible basis states of n qubits that form the quantum
state. The quantum state can include a global phase or relative phases among the
amplitudes of the basis states that do not affect the probabilities of measuring these
Because the number of possible basis states of a quantum state scales as as the number of qubits n grows, using
simulateis practical for finding the final state
sof a circuit only when n is less than about 15.
Introduced in R2023a