# getMatrix

Matrix representation of quantum circuit or gate

Since R2023a

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

## Description

example

M = getMatrix(c) returns the unitary matrix representation of a quantum circuit or gate. The size of the matrix is ${2}^{n}$-by-${2}^{n}$, where n is the largest qubit index of the circuit or gate.

## Examples

collapse all

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 $|00〉$, $|01〉$, $|10〉$, and $|11〉$, respectively. That is, the second column of the matrix represents the swap gate being applied to state $|01〉$, which results in state $|10〉$. The third column of the matrix represents the swap gate being applied to state $|10〉$, which results in state $|01〉$. For more information about basis states, see Basis States Definition.

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

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

## Input Arguments

collapse all

Quantum circuit or gate, specified as a quantumCircuit, SimpleGate, or CompositeGate object. The size of the returned matrix is ${2}^{n}$-by-${2}^{n}$, where n is the largest qubit index of the quantum circuit or gate.

## Limitations

• Because the size of the matrix that represents a quantum circuit or gate operation scales as ${2}^{n}$-by-${2}^{n}$ as the number of qubits n grows, using getMatrix is practical only when n is less than about 15.

collapse all

### Basis States Definition

• For a quantum circuit with n qubits, the overall ${2}^{n}$ 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 $|{q}_{k}〉$ (which can be $|0〉$ or $|1〉$), then the basis states of the circuit with n qubits are represented by $|{q}_{1}〉\otimes ...\otimes |{q}_{k}〉\otimes ...\otimes |{q}_{n}〉$. For example, for a circuit with two qubits, all possible basis states expressed as a column vector are $|{q}_{1}{q}_{2}〉={\left[|00〉,|01〉,|10〉,|11〉\right]}^{T}$. 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 ${2}^{n}$ 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 $|0〉$ state, this gate does nothing. If the control qubit is in the $|1〉$ state, this gate applies the Pauli X gate $\left[\begin{array}{cc}0& 1\\ 1& 0\end{array}\right]$ to the target qubit. The matrix representation of the controlled X gate is

$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& 0& 1\\ 0& 0& 1& 0\end{array}\right].$

## Tips

• To compare if two circuits perform an equivalent operation, you can use getMatrix to get the matrix representations of the circuits. If the two matrices are equal, then the circuits are equivalent.

## Version History

Introduced in R2023a