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 explore the U gate and how to implement it in Qiskit on IBM Quantum Computers.
What is the U gate?
The U gate is a gate that does a rotation around the Bloch sphere with 3 Euler angles. The three angles used to perform the rotations are θ,λ, and φ.
The operation of the U can be described with the following matrix:
The U gate can replicate any other single qubit gate. For example to replicate the Pauli-X gate you would rotate θ by π, φ by π and λ by π/2 :
Where U(π, π, π/2 ) = U(θ, φ, λ)
As an example lets replicate a Pauli-X gate and set the qubit state to |1〉To see how the U gate operates on the qubit we multiply the column vector associated with |1〉by the U gate matrix.
This is correct as the resulting column vector is for |0〉This shows that the qubits state has flipped from |1〉to |0〉
If we wanted to use a U gate to create a Hadamard gate then the parameters would be set like so:
Let’s prove this by setting the qubit to |0〉 and then by multiplying the state vector by the U gate matrix with the parameters set as above:
Which has put the qubit in to superposition just as the Hadamard gate would!
Implementation
In Qiskit the U gate can be implemented very easily with the following line of code:
circuit.u(theta, phi, lam,q[0])
Where theta, phi, and lam are the 3 Euler angles and q[0] is the qubit that the U gate is applied to.
For example lets say we want to replicate a Pauli-X gate using the U gate. We will need to set theta and phi to π and lambda to π/2 like so:
circuit.u(pi, pi, pi/2,q[0])
How to run the program
Copy and paste the code below in to a python file
Enter your API token in the IBMQ.enable_account('Insert API token here') part
Save and run
Code
from qiskit import QuantumRegister, ClassicalRegister from qiskit import QuantumCircuit, execute,IBMQ from qiskit.tools.monitor import job_monitor import numpy pi = numpy.pi IBMQ.enable_account('ENTER API KEY HERE') provider = IBMQ.get_provider(hub='ibm-q') backend = provider.get_backend('ibmq_qasm_simulator') q = QuantumRegister(1,'q') c = ClassicalRegister(1,'c') circuit = QuantumCircuit(q,c) circuit.u(pi,pi,pi/2,q[0]) # U3 Gate circuit.measure(q,c) # Qubit Measurment print(circuit) job = execute(circuit, backend, shots=8192) job_monitor(job) counts = job.result().get_counts() print(counts)