Commit d5753ea8 authored by josd's avatar josd

added

parent 7f3745bb
......@@ -2,4 +2,4 @@ from sympy import *
print('PREFIX : <http://josd.github.io/eye/imagining#>')
print('')
print('"E**(I*pi) + 1" :identical "%s".' % (E**(I*pi) + 1))
print('"E**(I*pi) + 1" :eval %d .' % (E**(I*pi) + 1))
PREFIX : <http://josd.github.io/eye/imagining#>
"E**(I*pi) + 1" :identical "0".
"E**(I*pi) + 1" :eval 0 .
......@@ -6,6 +6,6 @@ def fib(n, c={0:1, 1:1}):
print('PREFIX : <http://josd.github.io/eye/imagining#>')
print('')
print('0 :fib "%d".' % (fib(0)))
print('91 :fib "%d".' % (fib(91)))
print('283 :fib "%d".' % (fib(283)))
print('0 :fib %d .' % (fib(0)))
print('91 :fib %d .' % (fib(91)))
print('283 :fib %d .' % (fib(283)))
PREFIX : <http://josd.github.io/eye/imagining#>
0 :fib "1".
91 :fib "7540113804746346429".
283 :fib "100694286476841731898333719576864360661213863366454327287613".
0 :fib 1 .
91 :fib 7540113804746346429 .
283 :fib 100694286476841731898333719576864360661213863366454327287613 .
=== Grover's algorithm ===
# Original from https://github.com/ProjectQ-Framework/ProjectQ/blob/develop/examples/grover.py
# See also https://en.wikipedia.org/wiki/Grover%27s_algorithm
import math
from projectq import MainEngine
from projectq.ops import H, Z, X, Measure, All
from projectq.meta import Loop, Compute, Uncompute, Control
def run_grover(eng, n, oracle):
"""
Runs Grover's algorithm on n qubit using the provided quantum oracle.
Args:
eng (MainEngine): Main compiler engine to run Grover on.
n (int): Number of bits in the solution.
oracle (function): Function accepting the engine, an n-qubit register,
and an output qubit which is flipped by the oracle for the correct
bit string.
Returns:
solution (list<int>): Solution bit-string.
"""
x = eng.allocate_qureg(n)
# start in uniform superposition
All(H) | x
# number of iterations we have to run:
num_it = int(math.pi/4.*math.sqrt(1 << n))
# prepare the oracle output qubit (the one that is flipped to indicate the
# solution. start in state 1/sqrt(2) * (|0> - |1>) s.t. a bit-flip turns
# into a (-1)-phase.
oracle_out = eng.allocate_qubit()
X | oracle_out
H | oracle_out
# run num_it iterations
with Loop(eng, num_it):
# oracle adds a (-1)-phase to the solution
oracle(eng, x, oracle_out)
# reflection across uniform superposition
with Compute(eng):
All(H) | x
All(X) | x
with Control(eng, x[0:-1]):
Z | x[-1]
Uncompute(eng)
All(Measure) | x
Measure | oracle_out
eng.flush()
# return result
return [int(qubit) for qubit in x]
def alternating_bits_oracle(eng, qubits, output):
"""
Marks the solution string 1,0,1,0,...,0,1 by flipping the output qubit,
conditioned on qubits being equal to the alternating bit-string.
Args:
eng (MainEngine): Main compiler engine the algorithm is being run on.
qubits (Qureg): n-qubit quantum register Grover search is run on.
output (Qubit): Output qubit to flip in order to mark the solution.
"""
with Compute(eng):
All(X) | qubits[1::2]
with Control(eng, qubits):
X | output
Uncompute(eng)
if __name__ == "__main__":
eng = MainEngine() # use default compiler engine
# run Grover search to find a 7-bit solution
lst = run_grover(eng, 7, alternating_bits_oracle)
n3_lst = '(' + ' '.join([str(i) for i in lst]) + ')'
print('PREFIX : <http://josd.github.io/eye/imagining#>')
print('')
print('"grover(7_bits, alternating_bits_oracle)" :answer %s.' % (n3_lst))
PREFIX : <http://josd.github.io/eye/imagining#>
"grover(7_bits, alternating_bits_oracle)" :answer (1 0 1 0 1 0 1).
#!/bin/bash
python3 grover.py > grover.ttl
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment