Install Qiskit 2.0 and run a simple quantum circuit
Qiskit 2.0 has been released. In this article, we will explain how to run a simple quantum circuit using Qiskit 2.0.1.
Amazon Braket Learning Course
I have created a learning course on Amazon Braket, AWS’s quantum computing service.
This course is designed for those with no prior knowledge of quantum computing or AWS, and by the end, you’ll even be able to learn about quantum machine learning. Take advantage of this opportunity to build your skills in quantum technologies!
Install Qiskit 2.0.1 and run a simple quantum circuit.
At the time of writing, the latest version of Qiskit is 2.0.1. In this article, we will install this version and run a simple quantum circuit. The environment has been set up as follows:
pip install qiskit==2.0.1 notebook numpy
In this article, we will run a quantum circuit using the StatevectorSampler. We will also import other necessary modules along with it.
from qiskit.circuit import (
Parameter, QuantumCircuit
)
from qiskit.primitives import StatevectorSampler
Let’s define the sampler.
sampler = StatevectorSampler()
As usual, we define a quantum circuit. This time, we are creating a Bell circuit.
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0,1], [0,1])
Qiskit allows you to define multiple parameters together with a quantum circuit and execute them simultaneously using a sampler. In this example, since there are no parameter-dependent gates, we will proceed without defining any parameters by passing None.
pub = (qc, None)
job = sampler.run([pub], shots=100)
You can check the results as follows.
result = job.result()[0]
print(result.data.c.get_counts())
# {'11': 53, '00': 47}
Now, let’s proceed to execute a quantum circuit with multiple parameters simultaneously. We will define a circuit that uses rx and ry gates, which apply rotations dependent on the parameters x and y, as follows:
qc = QuantumCircuit(2, 2)
qc.rx(Parameter("x"), 0)
qc.ry(Parameter("y"), 1)
qc.measure([0,1], [0,1])
We define the two parameters over the range from 0 to 2$\pi$, with intervals of $\pi$/2, as shown below.
params = np.vstack([
np.linspace(0, 2 * np.pi, 5),
np.linspace(0, 2 * np.pi, 5)
]).T
print(params)
# [[0. 0. ]
# [1.57079633 1.57079633]
# [3.14159265 3.14159265]
# [4.71238898 4.71238898]
# [6.28318531 6.28318531]]
Using these parameters, we execute the quantum circuit as shown below.
sampler = StatevectorSampler()
pub = (qc, params)
job = sampler.run([pub], shots=100)
The execution results of the quantum circuit associated with each parameter set can be checked individually. For example, the result corresponding to the first parameter set (index 0) can be obtained as shown below.
result = job.result()[0]
print(result.data.c.get_counts(0))
# {'00': 100}
Similarly, we can check the other results. We can see that the outcomes for each parameter set behave as expected.
print(result.data.c.get_counts(1))
# {'01': 28, '00': 24, '11': 24, '10': 24}
print(result.data.c.get_counts(2))
# {'11': 100}
print(result.data.c.get_counts(3))
# {'10': 27, '01': 22, '00': 24, '11': 27}
Since we can define multiple parameters and execute them all at once, this approach seems particularly useful when running quantum machine learning algorithms like VQC.
Amazon Braket Learning Course
I have created a learning course on Amazon Braket, AWS’s quantum computing service.
This course is designed for those with no prior knowledge of quantum computing or AWS, and by the end, you’ll even be able to learn about quantum machine learning. Take advantage of this opportunity to build your skills in quantum technologies!