Quantum programming with IBM QX
Setting
You need Python 3.x or higher. The version I used is 3.7.7.
Packages that need to be additionally installed are numpy, matplotlib, qiskit, and pygame. Install them using the pip install command.
Because numpy 1.19.4 has a bug, so a sanity check error occurs in fmod function if you install the latest version. As of January 2021, there is no problem with 1.19.3 installed.
The current version of qiskit is 0.23.2.
The book’s codes are posted on github that can be run with jupyter notebook. However, the code needs to be updated; it uses old qiskit 0.7.0.
git clone https://github.com/PacktPublishing/Mastering-Quantum-Computing-with-IBM-QX
Visit the link below and create a new account to use IBM quantum service.
After you successfully get an account, then login to the site and select the “My account” menu. You can see the following screen.
Copy and save the token shown in section 2 of “Qiskit in local environment” shown on the right. This API token is used as an account key to access IBM quantum service in the program.
Now, let’s program a “Hello, World!” of quantum computing. It simply makes 5 qubits and measures it. When creating a qubit in quantum computing, the default value is ‘0’, which is represented as |0> in quantum mechanics. With 5 qubits, the result is expected to be ‘00000’, but in quantum mechanics, it should be said probabilistically. That is, theoretically, it is expected that ‘00000’ will come out with a 100% probability when you measure them. However, with a real quantum computer, there will be errors, so 100% is (almost) impossible.
This is the quantum circuit for the program.
Let’s create a program that executes the above circuit. Your cloned code from git hub needs to be modified referring to the code below.
import qiskit
from qiskit import IBMQ
from qiskit import Aer
import time
from qiskit.tools.visualization import plot_histogram# copy your API token
IBMQ.enable_account(“YOUR_API_TOKEN”)
print(IBMQ.providers()) # modified
# get provider to get backendprint(Aer.backends()) # local backends
provider = IBMQ.get_provider('ibm-q') # ibm-q in provider list
print(provider.backends()) # choose simulato
rbackend=provider.get_backend('ibmq_qasm_simulator')q = qiskit.QuantumRegister(5) # 5 qubit '0's# measuring turns them classical bits
c = qiskit.ClassicalRegister(5)
qc = qiskit.QuantumCircuit(q, c)
qc.measure(q, c) # results in classical bitsjob_exp = qiskit.execute(qc, backend=backend) # simulationplot_histogram(job_exp.result().get_counts(qc))
If you run it without a problem (there could be a warning), you get a histogram like this: ‘00000’ with probability 1.0.