Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.
Introduction
In this tutorial we will see how to obtain the unitary matrix of any quantum circuit in Qiskit.
In quantum computing unitary matrices are very important as they describe how quantum logic gates and circuits affect qubit states. However with large quantum circuits it can be difficult to derive the unitary matrix by hand.
However Qiskit thankfully has a function for obtaining unitary matrices for a circuit called result.get_unitary(experiment, decimals)
Where:
experiment: The circuit you want to obtain the unitary matrix for
decimals: The number of decimals within the matrix
Implementation
For our example we will try to derive the unitary matrix for a simple circuit that creates a Bell pair:
For more information on Bell pairs feel free to check out our tutorial on them here: https://quantumcomputinguk.org/tutorials/introduction-to-bell-states
Step 1: Import modules
Step 2: Initialise the Backend
The next step is to intialise the backend device. Since this is only a tutorial we will use the Aer unitary simulator:
backend = Aer.get_backend('unitary_simulator')
Step 3: Create the circuit
The next step is to create the circuit. This is just a two qubit circuit that creates a Bell pair by applying a Hadamard gate to qubit 0. Then a CNOT gate is applied such that qubit 0 is the control and qubit 1 is the target.
q = QuantumRegister(2,'q') c = ClassicalRegister(2,'c') circuit = QuantumCircuit(q,c) circuit.h(q[0]) circuit.cx(q[0],q[1])
Step 4: Execute the circuit and obtain the results
job = execute(circuit, backend, shots=8192) result = job.result()
Step 5: Get the unitary matrix
Finally we can print the unitary matrix for the circuit from the results with the following code:
print(result.get_unitary(circuit,3))
Where the circuit is the circuit we created and 3 is the decimal limit for each entry in the matrix.