Main Content

getMatrix

Matrix representation of quantum circuit or gate

Since R2023a

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

Description

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

example

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 2n-by-2n, 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 2n-by-2n as the number of qubits n grows, using getMatrix is practical only when n is less than about 15.

More About

collapse all

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