...

/

Calculating the Transformation Matrix

Calculating the Transformation Matrix

Learn how we can calculate the transformation matrix.

We'll cover the following...

Calculate the transformation matrix

In line 4, we create the QuantumCircuit with two qubits. Then, in lines 7 and 8, we apply the XX-gate to the one qubit and the HH-gate to the other.

Press + to interact
from qiskit import QuantumCircuit, Aer, execute
# Create a quantum circuit with one qubit
qc = QuantumCircuit(2)
# apply the Hadamard gate to the qubit
qc.i(0)
qc.h(1)
backend = Aer.get_backend('unitary_simulator')
unitary = execute(qc,backend).result().get_unitary()
# Display the results
print(unitary)

Note: Qiskit orders the qubits from back to front about the matrix calculation, so we need to switch the positions.

This time, in line 10, we use a different Qiskit simulator as the backend, the UnitarySimulator. This simulator executes the circuit once and returns the final transformation matrix of the circuit itself. Note that this simulator does not contain any measurements.

The result is the matrix our circuit represents.

What if we only wanted to apply the HH-gate to one of the qubits and leave the other unchanged? How would we calculate such a two-qubit transformation matrix?

We can use the ...