- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
While we might not be able to understand or utilize Quantum Computing in our daily lives fully, we cannot deny its importance in the future of computing. This blog post serves as a gentle introduction to get a better understanding and further explore its use.
Here’s our Coding with Nylas Livestream video to get a better understanding:
Quantum computers rely on naturally occurring quantum-mechanical phenomena, basically superposition and entanglement. We’ll go into detail about these two states in the following sections!
These two states can drastically increase computational speeds in complex sets of data, although they are not meant to be used on every computational problem.
While classical computers use bits, Quantum computers use Qubits:
In classical computers, a Bit can be either 0 or 1. In Quantum computers, a Qubit can be either 0 or 1, but it can also be both values simultaneously, which is called Superposition.
A Qubit is a two-dimensional vector space:
But of course, with Superposition, it can become:
Both Alpha and Beta are complex numbers and together they follow:
Entanglement means that a pair or group of particles are linked together, and they cannot be described separately. No matter if they are separated by a million years away.
In other words, let’s say we have two boxes, each containing a ball. The ball can be red, yellow or both colours at the same time. Let’s say we close the boxes, and take a look at the ball on the first box, if it’s yellow when we look at it, then the ball on the second box will be yellow as well. If we close both boxes and look at the ball in the second box first, and it happens to be red, then we can know that the ball in the first box will be red as well.
They can be both colours at the same time as well. This is critical concept to understand in Quantum Computing.
Quantum computers can handle a lot of data in a fast way. Although quantum computing is not meant for every kind of problem.
Its uses are:
Quantum Computers need an operating temperature of 15 millikelvin which is roughly equivalent to -237 Celcius or -459 Fahrenheit.
Gladly, we can and even when it’s just a simulation, we can do it locally:
$ pip3 install qiskit
Qiskit is a Python Open Source Framework that can handle up to 24 Qubits.
That might not look like much as IBM Quantum Computers can manage up to 50 Qubits, Alphabet’s can manage up to 72 Qubits and D-wave can manage up to 2,000 Qubits.
Of course, a D-Wave Quantum Computer can cost up to 1.5 million dollars, so being able to handle 24 Qubits is as good as it gets for now.
We might need to install some additional libraries:
$ pip3 install matplotlib $ pip3 install pillow
Qiskit is composed of four elements broken down as follows:
One of the most simple but at the same time most important examples in Quantum computing is the Bell State.The Bell State involves two Qubits and represents both Superposition and Entanglement. This means that if we measure a Qubit and get a result of 1, the other Qubit will measure 1 no matter what.
This is the basis for Quantum Communication and Quantum Teleportation.
Let’s create the following script and call it Quantum_Bell_State.py:
# Import libraries from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, BasicAer from qiskit.tools.visualization import * from PIL import Image import matplotlib.pyplot as plt q = QuantumRegister(2) # Two Qubits c = ClassicalRegister(2) # Two Bits bell_state = QuantumCircuit(q, c) # Quantum Circuit bell_state.h(q[0]) # Superposition. Hadamard Gate bell_state.cx(q[0], q[1]) # Entanglement. CNOT gate. bell_state.measure(q, c) # Measure value # Use the simulator to get the measurements job = execute(bell_state, backend = BasicAer.get_backend('qasm_simulator'), shots=1000) result = job.result() # Get the result back from the job bell_state.draw(output='mpl') # Draw the circuit plt.show() # Display the circuit fig = plot_histogram(result.get_counts(bell_state)) # Generate the image fig.savefig("bell_state.png") # Save the image image = Image.open("bell_state.png") # Open the image image.show() # Display the image
We can run this by typing on the terminal:
$ python3 Quantum_Bell_State.py
In this diagram, we can that we have two Qubits, and we apply a Hadamard gate first to create a Superposition state on both and then a CNot gate to create an Entanglement state, linking together both Qubits.
Finally, we measure both Qubits.
As we can see, we have a 48.7% chance that both Qubits are going to measure 0 and a 51.3% chance that both Qubits are going to measure 1.
We need to start learning what a Half Adder is. A Half Adder is an electronic circuit that performs the addition operation on numbers. While it can only sum two bits, it’s the foundation for more powerful operations.
A and B are the input bits. S is the sum and C is the carry.
1 + 1 equals 10 because that’s 2 in binary.
A Full Adder has 3 inputs and two outputs. Full Adders are great because they can be chained:
Let’s call this example Quantum_Full_Adder.py:
# Import libraries from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, BasicAer, execute from qiskit.tools.visualization import plot_histogram from qiskit.tools.visualization import * from PIL import Image import matplotlib.pyplot as plt q = QuantumRegister(5) # Five Qubits c = ClassicalRegister(5) # Five Bits qc = QuantumCircuit(q, c) # Quantum Circuit qc.x(q[0]) # Pauli X gate qc.x(q[1]) # Pauli X gate qc.ccx(q[0], q[1], q[3]) # Toffoli Gate qc.cx(q[0], q[1]) # CNot gate qc.x(q[2]) # Pauli X gate qc.ccx(q[1], q[2], q[4]) # Toffoli Gate qc.cx(q[1], q[2]) # CNot gate qc.cx(q[0], q[1]) # CNot gate qc.measure(q[2], c[0]) # Measure value qc.measure(q[1], c[1]) # Measure value # Use the simulator to get the measurements job = execute(qc, backend = BasicAer.get_backend('qasm_simulator'), shots=1000) result = job.result() # Get the result back from the job qc.draw(output='mpl') # Draw the circuit plt.show() # Display the circuit fig = plot_histogram(result.get_counts(qc)) # Generate the image fig.savefig("full_adder.png") # Save the image image = Image.open("full_adder.png") # Open the image image.show() # Display the image
On the terminal, we can run this:
$ python3 Quantum_Full_Adder.py
We have 3 Qubits, q00, q01 and q03. We apply a Pauli X Gate (X) to perform a bit-flip operation, which means changing their state from 0 to 1 on q00 and q01.
Next, we apply a Toffoli gate, which is pretty much an AND gate. It receives 2 inputs and returns 1 output. If the inputs are 1, it inverts the output, otherwise, it does nothing.
Then, we apply a CNOT (Controlled Not Gate), which is pretty much an XOR gate. The target will change only if the control is 1. q00 is the control and q01 is the target.
At this point, we have a Half Adder.
We apply a Pauli X Gate (X) on q02.
Next, we apply a Toffoli gate on q01 and q02 with a target on q04.
Then we apply a CNOT gate, where the control is q01 and the target is q02.
Finally, we apply a CNOT gate, where the control is q00 and the target is q01.
We measure the results coming from q01 and q02:
We know that 1 + 1 + 1 is going to be 3, because 0011 in binary is 3. And we can see that we have a 100% chance that this will happen.
Using Binary, we can represent letters, for example, The letter a is 01100001, which means that a single letter takes up to 8 bits.
Actually, by doing a bit of research, we can figure out that the first 3 bits 001 never change.
With that in mind, we can create a small application that will take 24 bits, that’s 3 characters and create 3 letter words.
Now, we need to make sure that the generated letters are valid, and that’s where the Enchant library comes in. To install it, do
$ pip3 install pyenchant
Note: If you are on a Mac, also install the enchant library using Brew, before installing the Python package:
$ brew install enchant
We’re going to call this file, Quantum_Words.py:
# Import libraries from qiskit import IBMQ, BasicAer, execute from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit import enchant # Load the US library d = enchant.Dict("en_US") q = QuantumRegister(24, 'q') # Twenty four Qubits c = ClassicalRegister(24, 'c') # Twenty four Bits qc = QuantumCircuit(q, c) # Quantum Circuit # First letter qc.h(q[0]) # Hadamard Gate qc.h(q[1]) # Hadamard Gate qc.h(q[2]) # Hadamard Gate qc.h(q[3]) # Hadamard Gate qc.h(q[4]) # Hadamard Gate qc.x(q[5]) # Pauli X gate qc.x(q[6]) # Pauli X gate # Second letter qc.h(q[8]) qc.h(q[9]) qc.h(q[10]) qc.h(q[11]) qc.h(q[12]) qc.x(q[13]) qc.x(q[14]) # Third letter qc.h(q[16]) qc.h(q[17]) qc.h(q[18]) qc.h(q[19]) qc.h(q[20]) qc.x(q[21]) qc.x(q[22]) # Measure all 24 Qubits for j in range(24): qc.measure(q[j], c[j]) # Use the simulator to get the measurements job = execute(qc, backend=BasicAer.get_backend('qasm_simulator'), shots=100) # Get the result back from the job result = job.result().get_counts(qc) characterDict = {} # Separate each result into 8 bits, turning them into letters for bitString in result: char1 = chr(int( bitString[0:8] ,2)) char2 = chr(int( bitString[8:16] ,2)) char3 = chr(int( bitString[16:24] ,2)) # Put all resulting words into an array characterDict[ char1 + char2 + char3 ] = result[bitString] / 1024 counter = 0 # Loop through all the generated words for char in characterDict.keys(): # Using pyenchant, find which ones are valid if d.check(char) == True: # Count the valid words counter += 1 # Print them out print(char) # Out of a 100, how many are actually valid? print(str(counter) + " valid words of a generated 100")
Starting from the right side, we create Superposition on all the values that can change using Hadamard gates.
For the ones that will not change, we can negate using a Pauli X Gate, as they need to be 1.
Once they are all defined, we simply measure them in order to get their results.
We read the generated binary numbers, separated by packs of 8 bits, turn them into letters and make words with them.
By using the enchant library, we can make sure they are valid words.
This application is slow, but we’re using 24 Qubits and trying to generate a 100 words.
Using Quantum Computing on non Quantum Computers is still slow but getting better over time.
We can run this by typing on the terminal:
$ python3 Quantum_Words.py
Note: If you’re on a Mac, you might get this error message:
While the enchant library is installed, its path is not always defined, so we need to use Brew to install it. But there’s a simple fix, just run the following:
$ PYENCHANT_LIBRARY_PATH=/opt/homebrew/lib/libenchant-2.dylib PYENCHANT_VERBOSE_FIND=x python3 Quantum_Words.py
Here we have generated 4 valid words:
While here on 2.
Hope you will find out that Quantum Computing is not that hard.
Like what you read but looking for some Nylas content, make sure to check our great selection of blog posts.
Why don’t you sign up Nylas for free and start building!
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.