# Examples¶

All of these example codes and more can be found on GitHub.

## Quantum Random Numbers¶

The most basic example is a quantum random number generator (QRNG). It can be found in the examples-folder of ProjectQ. The code looks as follows

from projectq.ops import H, Measure
from projectq import MainEngine

# create a main compiler engine
eng = MainEngine()

# allocate one qubit
q1 = eng.allocate_qubit()

# put it in superposition
H | q1

# measure
Measure | q1

eng.flush()
# print the result:
print("Measured: {}".format(int(q1)))


Running this code three times may yield, e.g.,

$python examples/quantum_random_numbers.py Measured: 0$ python examples/quantum_random_numbers.py
Measured: 0
$python examples/quantum_random_numbers.py Measured: 1  These values are obtained by simulating this quantum algorithm classically. By changing three lines of code, we can run an actual quantum random number generator using the IBM Quantum Experience back-end: $ python examples/quantum_random_numbers_ibm.py
Measured: 1
$python examples/quantum_random_numbers_ibm.py Measured: 0  All you need to do is: --- /home/docs/checkouts/readthedocs.org/user_builds/projectq/checkouts/v0.4.1/examples/quantum_random_numbers.py +++ /home/docs/checkouts/readthedocs.org/user_builds/projectq/checkouts/v0.4.1/examples/quantum_random_numbers_ibm.py @@ -1,8 +1,11 @@ +import projectq.setups.ibm from projectq.ops import H, Measure from projectq import MainEngine +from projectq.backends import IBMBackend # create a main compiler engine -eng = MainEngine() +eng = MainEngine(IBMBackend(), + engine_list=projectq.setups.ibm.get_engine_list()) # allocate one qubit q1 = eng.allocate_qubit()  ## Quantum Teleportation¶ Alice has a qubit in some interesting state $$|\psi\rangle$$, which she would like to show to Bob. This does not really make sense, since Bob would not be able to look at the qubit without collapsing the superposition; but let’s just assume Alice wants to send her state to Bob for some reason. What she can do is use quantum teleportation to achieve this task. Yet, this only works if Alice and Bob share a Bell-pair (which luckily happens to be the case). A Bell-pair is a pair of qubits in the state $|A\rangle \otimes |B\rangle = \frac 1{\sqrt 2} \left( |0\rangle\otimes|0\rangle + |1\rangle\otimes|1\rangle \right)$ They can create a Bell-pair using a very simple circuit which first applies a Hadamard gate to the first qubit, and then flips the second qubit conditional on the first qubit being in $$|1\rangle$$. The circuit diagram can be generated by calling the function def create_bell_pair(eng): b1 = eng.allocate_qubit() b2 = eng.allocate_qubit() H | b1 CNOT | (b1, b2) return b1, b2  with a main compiler engine which has a CircuitDrawer back-end, i.e., from projectq import MainEngine from projectq.backends import CircuitDrawer from teleport import create_bell_pair # create a main compiler engine drawing_engine = CircuitDrawer() eng = MainEngine(drawing_engine) create_bell_pair(eng) eng.flush() print(drawing_engine.get_latex())  The resulting LaTeX code can be compiled to produce the circuit diagram: $ python examples/bellpair_circuit.py > bellpair_circuit.tex
$pdflatex bellpair_circuit.tex  The output looks as follows: Now, this Bell-pair can be used to achieve the quantum teleportation: Alice entangles her qubit with her share of the Bell-pair. Then, she measures both qubits; one in the Z-basis (Measure) and one in the Hadamard basis (Hadamard, then Measure). She then sends her measurement results to Bob who, depending on these outcomes, applies a Pauli-X or -Z gate. The complete example looks as follows:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 from projectq.ops import All, CNOT, H, Measure, Rz, X, Z from projectq import MainEngine from projectq.meta import Dagger, Control def create_bell_pair(eng): b1 = eng.allocate_qubit() b2 = eng.allocate_qubit() H | b1 CNOT | (b1, b2) return b1, b2 def run_teleport(eng, state_creation_function, verbose=False): # make a Bell-pair b1, b2 = create_bell_pair(eng) # Alice creates a nice state to send psi = eng.allocate_qubit() if verbose: print("Alice is creating her state from scratch, i.e., |0>.") state_creation_function(eng, psi) # entangle it with Alice's b1 CNOT | (psi, b1) if verbose: print("Alice entangled her qubit with her share of the Bell-pair.") # measure two values (once in Hadamard basis) and send the bits to Bob H | psi Measure | psi Measure | b1 msg_to_bob = [int(psi), int(b1)] if verbose: print("Alice is sending the message {} to Bob.".format(msg_to_bob)) # Bob may have to apply up to two operation depending on the message sent # by Alice: with Control(eng, b1): X | b2 with Control(eng, psi): Z | b2 # try to uncompute the psi state if verbose: print("Bob is trying to uncompute the state.") with Dagger(eng): state_creation_function(eng, b2) # check whether the uncompute was successful. The simulator only allows to # delete qubits which are in a computational basis state. del b2 eng.flush() if verbose: print("Bob successfully arrived at |0>") if __name__ == "__main__": # create a main compiler engine with a simulator backend: eng = MainEngine() # define our state-creation routine, which transforms a |0> to the state # we would like to send. Bob can then try to uncompute it and, if he # arrives back at |0>, we know that the teleportation worked. def create_state(eng, qb): H | qb Rz(1.21) | qb # run the teleport and then, let Bob try to uncompute his qubit: run_teleport(eng, create_state, verbose=True)  and the corresponding circuit can be generated using $ python examples/teleport_circuit.py > teleport_circuit.tex
$pdflatex teleport_circuit.tex  which produces (after renaming of the qubits inside the tex-file): ## Shor’s algorithm for factoring¶ As a third example, consider Shor’s algorithm for factoring, which for a given (large) number $$N$$ determines the two prime factor $$p_1$$ and $$p_2$$ such that $$p_1\cdot p_2 = N$$ in polynomial time! This is a superpolynomial speed-up over the best known classical algorithm (which is the number field sieve) and enables the breaking of modern encryption schemes such as RSA on a future quantum computer. A tiny bit of number theory There is a small amount of number theory involved, which reduces the problem of factoring to period-finding of the function $f(x) = a^x\operatorname{mod} N$ for some a (relative prime to N, otherwise we get a factor right away anyway by calling gcd(a,N)). The period r for a function f(x) is the number for which $$f(x) = f(x+r)\forall x$$ holds. In this case, this means that $$a^x = a^{x+r}\;\; (\operatorname{mod} N)\;\forall x$$. Therefore, $$a^r = 1 + qN$$ for some integer q and hence, $$a^r - 1 = (a^{r/2} - 1)(a^{r/2}+1) = qN$$. This suggests that using the gcd on N and $$a^{r/2} \pm 1$$ we may find a factor of N! Factoring on a quantum computer: An example At the heart of Shor’s algorithm lies modular exponentiation of a classically known constant (denoted by a in the code) by a quantum superposition of numbers $$x$$, i.e., $|x\rangle|0\rangle \mapsto |x\rangle|a^x\operatorname{mod} N\rangle$ Using $$N=15$$ and $$a=2$$, and applying this operation to the uniform superposition over all $$x$$ leads to the superposition (modulo renormalization) $|0\rangle|1\rangle + |1\rangle|2\rangle + |2\rangle|4\rangle + |3\rangle|8\rangle + |4\rangle|1\rangle + |5\rangle|2\rangle + |6\rangle|4\rangle + \cdots$ In Shor’s algorithm, the second register will not be touched again before the end of the quantum program, which means it might as well be measured now. Let’s assume we measure 2; this collapses the state above to $|1\rangle|2\rangle + |5\rangle|2\rangle + |9\rangle|2\rangle + \cdots$ The period of a modulo N can now be read off. On a quantum computer, this information can be accessed by applying an inverse quantum Fourier transform to the x-register, followed by a measurement of x. Implementation There is an implementation of Shor’s algorithm in the examples folder. It uses the implementation by Beauregard, arxiv:0205095 to factor an n-bit number using 2n+3 qubits. In this implementation, the modular exponentiation is carried out using modular multiplication and shift. Furthermore it uses the semi-classical quantum Fourier transform [see arxiv:9511007]: Pulling the final measurement of the x-register through the final inverse quantum Fourier transform allows to run the 2n modular multiplications serially, which keeps one from having to store the 2n qubits of x. Let’s run it using the ProjectQ simulator: $ python3 examples/shor.py

projectq
--------
Implementation of Shor's algorithm.
Number to factor: 15

Factoring N = 15: 00000001

Factors found :-) : 3 * 5 = 15


Simulating Shor’s algorithm at the level of single-qubit gates and CNOTs already takes quite a bit of time for larger numbers than 15. To turn on our emulation feature, which does not decompose the modular arithmetic to low-level gates, but carries it out directly instead, we can change the line

 86 87 88 89 90 91 92 93 94 95 96 97 98 99 # Filter function, which defines the gate set for the first optimization # (don't decompose QFTs and iQFTs to make cancellation easier) def high_level_gates(eng, cmd): g = cmd.gate if g == QFT or get_inverse(g) == QFT or g == Swap: return True if isinstance(g, BasicMathGate): return False if isinstance(g, AddConstant): return True elif isinstance(g, AddConstantModN): return True return False return eng.next_engine.is_available(cmd) 

in examples/shor.py to return True. This allows to factor, e.g. $$N=4,028,033$$ in under 3 minutes on a regular laptop!

The most important part of the code is

 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69  for k in range(2 * n): current_a = pow(a, 1 << (2 * n - 1 - k), N) # one iteration of 1-qubit QPE H | ctrl_qubit with Control(eng, ctrl_qubit): MultiplyByConstantModN(current_a, N) | x # perform inverse QFT --> Rotations conditioned on previous outcomes for i in range(k): if measurements[i]: R(-math.pi/(1 << (k - i))) | ctrl_qubit H | ctrl_qubit # and measure Measure | ctrl_qubit eng.flush() measurements[k] = int(ctrl_qubit) if measurements[k]: X | ctrl_qubit 

which executes the 2n modular multiplications conditioned on a control qubit ctrl_qubit in a uniform superposition of 0 and 1. The control qubit is then measured after performing the semi-classical inverse quantum Fourier transform and the measurement outcome is saved in the list measurements, followed by a reset of the control qubit to state 0.