WARNING: IBM has recently updated their interfaces and the Qiskit library so many "older" resources you find online (i.e. older than a year) likely have out-of-date syntax. You can still use any ideas you get from those sources, but you will have to update the code.

The following code will need to be run locally and to do so you will need to install (at least) three libraries: qiskit, quiskit-ibm-runtime, and qiskit-aer. These can easily be installed using pip but if you need other installation methods see here for the first two and here for Aer.

You will also need to create a free IBM Quantum account through this link. Once you have created an account you will need to copy the API Token that is given in the upper right-hand corner. Note that the IBM account only gives you 10 minutes a month of real quantum computer usage (per account...). You can keep track of the amount of time you have used on the IBM Quantum dashboard (the same page you find your token).

First, we want to build a simple circuit that will create a two-qubit Bell state. The Bell state is restricted to both qubits being in the "up" position (represented by the zeros) or both qubits being in the "down" position (represented by the ones). There is an equal chance that our Bell state will be either both ups or both downs. The below wavefunction mathematically represents the Bell state we are trying to create.

First, we want to create the circuit and run it on a simulator quantum computer with no noise. Make sure all of the following imports work before moving further into the code. You may need to install some dependencies depending on how your computer is already set up.

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator

from qiskit_aer import AerSimulator

from qiskit.visualization import plot_histogram

from qiskit import transpile

If the above imports work, then we can start building our quantum circuit. The below code is the qiskit version of our Bell state shown above. We apply a Hadamard gate to quibit 0 and then a control-X gate between quibit 0 and quibit 1.

# Create a Quantum Circuit acting on the q register

circuit = QuantumCircuit(2, 2)

# Add a H gate on qubit 0


# Add a CX (CNOT) gate on control qubit 0 and target qubit 1

circuit.cx(0, 1)

# Map the quantum measurement to the classical bits

circuit.measure([0,1], [0,1])


Deliverable 4: Explain what Hadamard and control-X gates are and how they can be combined as in this quantum circuit to create our desired Bell state.

Now let's simulate the code without any noise at all. We can do so using the below code, where the first line sets up our quantum computer simulator, the second line runs the circuit we created above on the simulator, and the third line creates a histogram of the results. Note that this code (by default) creates and measures the bell state 1,024 times and plots all of the results.

simulator = AerSimulator()

results = simulator.run(circuit).result().get_counts()


Deliverable 5: Run the above code several times to get a thorough statistic on the fraction of times the code returns the "up-up" state. Compare this fraction to the theoretical result.

Next, let's continue to run our code on the simulated quantum computer but add in some realistic noise based on an actual quantum computer. For this code, you will need the API token you have access to on your IBM dashboard. Make sure you replace the YOUR TOKEN string in the below code with your unique token. The first line of the below code connects your code to your IBM account, the second line connects you specifically to the IBM quantum computer in Kyoto, Japan (though you can change this to any of the IBM quantum computers), the third line sets up the simulator and the last two lines run our circuit on this noisy simulation and creates a histogram of the results.

# Construct a simulator using a noise model from a real backend.

provider = QiskitRuntimeService(channel="ibm_quantum", token="YOUR TOKEN")

backend = provider.get_backend("ibm_kyoto")

aersim_backend = AerSimulator.from_backend(backend)

# Perform noisy simulation

results = aersim_backend.run(circuit).result().get_counts()


Deliverable 6: Instead of just the two desired states, you should see some additional states. Where do these states come from and how can we explain them?

Deliverable 7: Run this simulation a few times and get some statistics on the fraction of times the circuit ends up in the "up-up" state and the fraction of times it ends up in the "noisy" states.

Deliverable 8: Change the backend of your simulation to 2-3 other quantum computers. Is the noise comparable across the different computers tested? It is known that different noise levels exist across different quantum computers so if you are working on a big quantum computing project it will be best to always work on the same quantum computer.

Now it is time to run the code on a real quantum computer! Again, you will need to replace the "YOUR TOKEN" in the below code with your actual IBM token. Note the second line of the below code tells it to use the quantum computer with the shortest queue times, but you are welcome to change this to a specific quantum computer. NOTE: the queue times for a quantum computer can be quite long at times so do not wait until the night before the project is due to attempt this.

provider = QiskitRuntimeService(channel="ibm_quantum", token="YOUR TOKEN")

backend = provider.least_busy(simulator=False, operational=True)

qc_basis = transpile(circuit,backend)

results = backend.run(qc_basis).result().get_counts()


Note that here we need to "transpile" our quantum circuit before passing it to the quantum computer for the quantum computer to be able to understand it.

Deliverable 9: Run this simulation a few times and get some statistics on the fraction of times the circuit ends up in the "up-up" state and the fraction of times it ends up in the "noisy" states.

Deliverable 10: How do your results from Deliverable 9 compare to Deliverable 7? Was the noise in our noisy simulation an accurate representation?

Deliverable 11: The Bell state used in this code was only one of four Bell states (see here). Create a quantum circuit to make one of the other Bell states and repeat this experiment with it (i.e. running it on a noiseless simulation, a noisy simulation, and a real quantum computer).