Checking the unitary matrix of the quantum circuit on Qiskit

To create an application on a quantum computer, we have to create a quantum circuit which is a combination of quantum gates. We usually start all the qubits from 0 value and applying these quantum gates we finally get the result of the calculation as a sampling of the circuit.

On the background of this process we have “State Vector” as the state of the qubits and “Unitary Matrix” as the operation of the quantum circuit itself.

Sometimes we need to check the process what kind of operation is going on with these quantum gates. As a small problems we can check the process by checking the unitary matrix.

Now we see the process using Qiskit which is a famous quantum computing tools on python.

https://qiskit.org/

The quantum circuit

The circuit is like,

import numpy as np
from qiskit import *
from qiskit import Aer

backend_sim = Aer.get_backend('qasm_simulator')

#prepare the circuit
circ = QuantumCircuit(1,1)
#hadamard gate
circ.h(0)
#the measurement
circ.measure(range(1),range(1))

#job execution
job_sim = execute(circ, backend_sim, shots=1024)
result_sim = job_sim.result()
counts = result_sim.get_counts(circ)
print(counts)

We can get only the final result of the circuit as,

{'1': 474, '0': 550}

Now we want to see the operation inside.

Getting the unitary matrix

import numpy as np
from qiskit import *
from qiskit import Aer

#Changing the simulator
backend = Aer.get_backend('unitary_simulator')

#The circuit without measurement
circ = QuantumCircuit(1)
circ.h(0)

#job execution and getting the result as an object
job = execute(circ, backend)
result = job.result()

#get the unitary matrix from the result object
print(result.get_unitary(circ, decimals=3))

We now get,

[[ 0.707+0.j  0.707+0.j]
[ 0.707+0.j -0.707+0.j]]

This is the unitary matrix of the quantum circuit. We can check the process looking at this matrix.

import numpy as np
from qiskit import *
from qiskit import Aer

backend = Aer.get_backend('unitary_simulator')
#prepare 2qubits
circ = QuantumCircuit(2)
circ.h(0)
circ.x(1)

job = execute(circ, backend)
result = job.result()
print(result.get_unitary(circ, decimals=3))

Now we have 2qubits and the unitary matrix becomes bigger 4*4 size

[[ 0.   +0.j  0.   +0.j  0.707+0.j  0.707+0.j]
[ 0. +0.j 0. +0.j 0.707+0.j -0.707+0.j]
[ 0.707+0.j 0.707+0.j 0. +0.j 0. +0.j]
[ 0.707+0.j -0.707+0.j 0. +0.j 0. +0.j]]

We can only check a small unitary matrix on our computer and finally we cannot get the bigger matrix, so it is just a small check on a small problems.

--

--