Matrix representation of quantum circuit or gate
Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.
Matrix Representation of Simple Gate
Construct a simple swap gate that acts on two target qubits with indices 1 and 2.
g = swapGate(1,2);
Get the matrix representation of the swap gate.
M = getMatrix(g)
M = 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1
The rows and columns of this matrix each represent the basis states , , , and , respectively. That is, the second column of the matrix represents the swap gate being applied to state , which results in state . The third column of the matrix represents the swap gate being applied to state , which results in state . For more information about basis states, see Basis States Definition.
Matrix Representation of 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);
Find the matrix representation of the circuit.
M = getMatrix(c)
M = 0.7071 0 0.7071 0 0 0.7071 0 0.7071 0 0.7071 0 -0.7071 0.7071 0 -0.7071 0
Matrix Representation of Composite Gate
Create an array of inner gates that consists of a Pauli X gate, a Hadamard gate, and a swap gate.
gates = [xGate(1); hGate(2); swapGate(1,2)]
gates = 3×1 SimpleGate array with gates: Id Gate Control Target 1 x 1 2 h 2 3 swap [1,2]
Construct a composite gate from the array of inner gates. The composite gate acts on qubits 1 and 3 of the outer circuit containing this gate.
cg = compositeGate(gates,[1 3]);
Get the matrix representation of the composite gate.
M = getMatrix(cg)
M = 0 0 0 0 0.7071 0.7071 0 0 0.7071 0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 0.7071 0 0 0.7071 0.7071 0 0 0 0 0 0 0 0 0.7071 -0.7071 0 0 0.7071 -0.7071 0 0 0 0 0 0 0 0 0 0 0 0 0.7071 -0.7071 0 0 0.7071 -0.7071 0 0 0 0
c — Quantum circuit or gate
quantumCircuit object |
SimpleGate object |
Quantum circuit or gate, specified as a
CompositeGate object. The size of
the returned matrix is -by-, where n is the largest qubit index of the quantum
circuit or gate.
Because the size of the matrix that represents a quantum circuit or gate operation scales as -by- as the number of qubits n grows, using
getMatrixis practical only when n is less than about 15.
Basis States Definition
For a quantum circuit with n qubits, the overall basis states are constructed from the Kronecker product of the n qubit bases with ordering from left to right for qubits with the lowest index to the highest index. In other words, if the basis of a single qubit with index k is labeled as (which can be or ), then the basis states of the circuit with n qubits are represented by . For example, for a circuit with two qubits, all possible basis states expressed as a column vector are . This definition follows most textbooks.
The matrix representation of a circuit or gate operates on a quantum state that is represented by the linear combinations of the basis states using the above ordering. For example, consider the controlled X gate that operates on a target qubit (with index 2) based on the state of a control qubit (with index 1). If the control qubit is in the state, this gate does nothing. If the control qubit is in the state, this gate applies the Pauli X gate to the target qubit. The matrix representation of the controlled X gate is
To compare if two circuits perform an equivalent operation, you can use
getMatrixto get the matrix representations of the circuits. If the two matrices are equal, then the circuits are equivalent.
Introduced in R2023a