We'll dive right in and assume no experience with programming quantum computers or working with quantum physics. In the previous post, I described the difference between computers' digital transistor logic bit and the quantum computer qubit. Now let's dive in and examine the qubit.
All information in the quantum computer is expressed as state vectors using Dirac notation. Please don't get frustrated with the terminology. We're going to walk through this using simple examples. To learn quantum computing, you need to think in the mindset of linear algebra.
For example, a qubit is simply a two-element matrix. A qubit of state 0 is merely the matrix (1 0). Likewise, a qubit of state 1 is the matrix (0 1). These two matrices represent linear algebra vectors. Since these values represent the qubit's state, 0 or 1, these are called quantum state vectors.
Dirac and Bra-Ket Notation
Quantum physics uses special notation to represent these quantum state vectors called Dirac notation. The Dirac is also called bracket or bra-ket notation, as it is commonly referred to in quantum mechanics. The bra-ket notation uses the brackets, bra <, ket >, and a vertical bar, |, to compose quantum state vectors. But, again, please keep it simple. We're only dealing with basic linear algebra.
In bra-ket notation, a bra represents a row vector, and the ket represents the column vector. For example,
Figure 1. Bra-ket notation and their vectors.
For quantum computer programming, a single qubit is represented using ket notation. All qubit operations in quantum computing are unitary linear algebra operations. The Quantum Processing Unit (QPU) speaks linear algebra natively. This architecture paradigm is the fork in the road split between classical computers, with CPUs using binary digital logic, and quantum computers using QPUs and unitary operators. QPUs use quantum circuit programming instead of machine code programming. A quantum circuit consists of one or more unitary operators on a qubit or sequence of qubits. The QPU native data representation is state vectors - arrays.
Examining the Qubits in Qiskit
The code I use here is available on my GitHub site for this blog. You will need to download the file Entangled_01.ipynb. I recommend using Google Colab for these tutorials. You can use it online, and the IBM Qiskit and Python will run in a browser, virtual machine, on Google Colab.
Step 1. Go ahead and set up your account on Google Colab. If you have a Google login, then setup will be relatively easy.
Step 2. Download the notebook workspace from GitHub to your computer.
Entangled_01.ipynb
Step 3. After downloading the example source code from GitHub, from Google Colab, select Upload (far right top corner, see screen shot below) and install the Entangled_01.ipynb workspace from your computer. The browser will ask you to select the file from your computer.
That's it for loading the source code. Now we can start playing with Qubits and very basic quantum circuits.
Playing with Qubits in Qiskit
Let's install the IBM Qiskit virtual environment into our Google Colab workspace. This environment is a little bit snazzier than using Jupyter project. But it is pretty much the same function-wise.
Step 1. Press the play button icon to install the virtual machine settings.
This Python pip installation will take a minute or less to install. When completed, move on to step 2 to import the Python and Qiskit libraries we'll use for these examples.
Step 2. Install the Qiskit Python libraries.
Hit the play button to load the Qiskit libraries into your environment. Press the play button icon as shown in the top left of the screen shot.
Step 3. Load your local libraries. This code area is the section we will modify when we develop and update our libraries. Press the play button icon to load. This section contains the libraries we'll use and add library code as we progress in our quantum computer Qiskit skills.
Now you should have all the Qiskit and Python libraries installed in your virtual environment so we can start looking at qubits and basic quantum programming.
Let's Code!
In classical computer programming, our very first program is usually a simple, 'Hello World\n' style of program. Some quantum computing sites call their first program the 'Hello World' tutorial. But this is misleading since quantum computer programming is an entirely different programming paradigm. The QPU only speaks unitary matrix operations on qubits. Therefore, our first program must demonstrate a basic unitary operation on a single qubit.
Let's walk through the Python code and see what it is doing.
# Example 1: Create a basic single qubit circuit with state 0.
# MAIN ------------------------
# initialize the simulator
simulator = QasmSimulator()
# create the basic single quit circuit |0>
qubitState = '0'
numQubits = 1
circuit = InitializeCircuit(qubitState,numQubits)
# draw the circuit
style = {'backgroundcolor': 'lavender'}
circuit.draw(output='mpl', style = style)
First, we must initialize the Qiskit quantum computer simulator,
# initialize the simulator
simulator = QasmSimulator()
Next we initialize the qubit to state 0.
I am using a custom routine which I use to initialize qubits.
# create the basic single quit circuit |0>
qubitState = '0'
numQubits = 1
circuit = InitializeCircuit(qubitState,numQubits)
We're
initializing a single qubit for this example. But if we were want to
initialize a three qubits state, |100>, we would pass in qubitState =
'100', and numQubits = 3. This function call returns a basic quantum circuit. It doesn't do anything useful at the moment, except to demonstrate how to initialize Qiskit.
We can render our basic circuit we created using Python call to matplotlib.
# draw the circuit
style = {'backgroundcolor': 'lavender'}
circuit.draw(output='mpl', style = style)
When we run the code in Example 1, it will generate a rendering of our simple quantum circuit. This approach is a simple way to design and examine quantum circuits before implementing your code to the circuit.
Looking at our Example 1 output, we have initialized one qubit in our circuit to state 0. We also have set up one classical computer register, C, to receive data output from the QPU. Register C is a standard binary software register with bits 0 or 1. In this example, no data is transferred to C because we have performed any measurements on our qubit of state 0. Remember, the qubit state is a vector (an array data type) and not a binary value.
Understanding Qubits and State Vectors
Now let's move to Example 2 and look at how Qiskit represents state vectors in Python. Qubit states are represented as column state vectors. Qubits are denoted in Dirac (bra-ket) notation, |0>. The qubits use "ket" notation which means these are column vectors. The Qiskit data structure is a 2-element array for the 2-element column state vector. The figure below shows the qubit using Dirac "Ket" notation and the corresponding mathematical column vector. To the right of the column vector is the Qiskit array data structure representation. The letter j in the Qiskit array represents the letter i, for imaginary number. Qubits are real complex numbers.
Let's try out the lab exercise in Example 2 and examine the state vector output of our basic quantum circuit.
out_state = RunCircuit(circuit)
print(out_state) # Display the output state vector
We run our circuit, and the result is in the out_state variable, a Qiskit state vector. When we call the Pyhton print command it displays the contents of the state vector. Does it match with our state vector figure above?
Statevector([1.+0.j, 0.+0.j],
dims=(2,)) In Example 3, we repeat everything we've learned so far. This time, we will run our lab experiment with a qubit of state |1>. What do you think the state vector results will look like based on our figure above?
qubitState = '1'
numQubits = 1
circuit = InitializeCircuit(qubitState,numQubits)
out_state = RunCircuit(circuit)# create the basic single quit circuit |1>
print(out_state) # Display the output state vector
When we run Example 3 code, we get the following state vector for qubit state |1>.
Statevector([0.+0.j, 1.+0.j],
dims=(2,))Now is a good time to familiarize yourself with how Qiskit represents qubits. The basic data structure of Qiskit is the array because QPU has unitary operations performed on the qubit.
Lab - Qiskit Endians
Modify Example 3 code with the following:
qubitState = '100'
numQubits = 3
Examine the state vector of this 3 qubit configuration.
Replace the print(outstate) command and display the circuit.
Look at the state vector in the circuit diagram output, |psi> = [0,0,1]. The 3 qubit we state vector we initialized was |100>. Qiskit uses little endian ordering for both classical registers and qubits. Endianness is how computers store bytes and values in memory.
For our classical computer register bits, for example, we will have 3-bits for our 3 qubits.C= C0 C1 C2, respectively.
C = 011 (actual bits, not qubits)
C0 = 1
C1 = 1
C2 = 0
Qiskit uses little endian for qubits. Remember, we are working in unitary operations, more specifically with tensors.
For a 3-qubit wave function, we have:
|𝜓⟩=|𝐴⊗𝐵⊗𝐶⟩=|𝐴𝐵𝐶⟩
q0 =|𝐶⟩
q1 =|𝐵⟩
q2 =|𝐴⟩
Example: 3-qubit = |100>
q0 = 0
q1 = 0
q2 = 1
As is the case with our lab exercise. Compare for yourself.
For a quick math refresher, I highly recommend this book.

Time for a Brain Break
We've covered a lot. I am approaching from the standpoint that you're either a high school student or a
seasoned programming professional who wants to learn programming quantum computers. I took an
intensive certification program in Quantum Communication back in October of 2022. There was a lot to
digest in this material mentally. Too many assumptions were made that this technology and its implementation
were simplistic to even the most casual programmer.
Moreover, the training was overlaid in academic lingo corporations would need help understanding and embracing.
That was my walk away with the academic level certification. In this blog, I desire to bring this technology
to the level a high schooler (not MIT-bound) or a hobbyist programmer could understand and learn to code. So
play with the labs and learn the basics. You'll be entangling qubits and ripping virtual wormholes in no time. In
future posts, we'll continue building upon what we've learned today.
Comments
Post a Comment