- Any computer with Python 3

python3 -m pip install --upgrade pip python3 -m pip install --user projectq

In a text editor, create a file named
**q1.py** containing this code:

Run the program.

from projectq.ops import Measure from projectq import MainEngine quantum_engine = MainEngine() print("Default Qubit in state |0>", end=" ") for i in range(10): qubit = quantum_engine.allocate_qubit() Measure | qubit quantum_engine.flush() print(int(qubit), end=" ") print()

A newly created Qubit is in the state
** |0>**,
which means that
measuring the state of this Qubit
always results in 0,
as shown below.

Run the program.

from projectq.ops import Measure, X from projectq import MainEngine quantum_engine = MainEngine() print("Qubit in state |0>", end=" ") for i in range(10): qubit = quantum_engine.allocate_qubit() Measure | qubit quantum_engine.flush() print(int(qubit), end=" ") print() print("Qubit in state |1>", end=" ") for i in range(10): qubit = quantum_engine.allocate_qubit() X | qubit Measure | qubit quantum_engine.flush() print(int(qubit), end=" ") print()

the X gate flips the Qubit to
the state
** |1>**.
Measuring that Qubit always
results in 1,
as shown below.

`|0>`

`|1>`

The Hadamard gate moves the Qubit into
a superposition state containing
equal amounts of
** |0>**
and

`|1>`

In a text editor, create a file named
**q3.py** containing this code:

Run the program several times. The superposition state has a 50% chance of being 0 when measured, and a 50% chance of being 1, as shown below.

from projectq.ops import Measure, H from projectq import MainEngine quantum_engine = MainEngine() print("Qubit in state |0>", end=" ") for i in range(10): qubit = quantum_engine.allocate_qubit() Measure | qubit quantum_engine.flush() print(int(qubit), end=" ") print() print("Superposition State: ", end=" ") for i in range(10): qubit = quantum_engine.allocate_qubit() H | qubit Measure | qubit quantum_engine.flush() print(int(qubit), end=" ") print()

This is "quantum uncertainty" and it is a fundamental aspect of reality that changed everything about physics. It bothered Einstein, who said "God does not play dice." However, as far as we can tell, Einstein was wrong and this uncertainty is a real property of quantum objects.

To entangle Qubits, we use the CNOT operator.

In a text editor, create a file named
**q4.py** containing this code:

Run the program several times. The Qubits are sometimes 0, sometimes 1, but they are always equal, as shown below.

from projectq.ops import Measure, H, CNOT from projectq import MainEngine eng = MainEngine() for i in range(10): qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() H | qubit1 CNOT | (qubit1, qubit2) Measure | qubit1 Measure | qubit2 eng.flush() print("(", int(qubit1), int(qubit2), ")", end=" ") print()

The installation took 15 minutes on my system.

brew cask install mactex

In a text editor, create a file named
**q5.py** containing this code:

Execute these commands to write a LaTeX file and convert it to a PDF file:

from projectq.ops import Measure, H, CNOT from projectq import MainEngine from projectq.backends import CircuitDrawer drawing_engine = CircuitDrawer() eng = MainEngine(drawing_engine) qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() H | qubit1 CNOT | (qubit1, qubit2) eng.flush() print(drawing_engine.get_latex())

Browse to q5.pdf and double-click it. You see a pretty diagram of your quantum circuit, as shown below.

python3 q5.py > q5.tex pdflatex q5.tex

The two Qubits start in their
default states of
** |0>** .

One of them passes through a Hadamard
gate, and then they are entangled,
denoted by the the
**⊕**
symbol.

## C 510.1: Three Qubits (10 pts)

Program the quantum circuit shown below.Note that the

⊕symbol on a single line denotes an X gate.Run that circuit a few times, printing the three bits. The answers vary.

Find the answer containing the largest number of ones, and concatenate the bits, like this:

That's the flag.

001

## C 510.2: Five Qubits (10 pts)

Program the quantum circuit shown below.Note that the

⊕symbol on a single line denotes an X gate.Run that circuit a few times, printing the five bits. The answers vary.

Interpret each sequence of five bits into a binary number, and convert it to decimal.

The highest possible value is the flag.

For example, if the bits were

the flag would be 31.

11111

Quantum Computer Programming Introduction

Tutorial: Getting started with Quantum Computing in Python

Quantum teleportation

ProjectQ Docs Examples

Posted 7-12-2020 by Sam Bowne