Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self):
BasicEngine.__init__(self)
self._l = [[]]
def receive(self, command_list):
for cmd in command_list:
if not cmd.gate == FlushGate():
self.cache_cmd(cmd)
if not self.is_last_engine:
self.send(command_list)
# 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]
"""
n = int(math.ceil(math.log(N, 2)))
x = eng.allocate_qureg(n)
X | x[0]
measurements = [0] * (2 * n) # will hold the 2n measurement results
ctrl_qubit = eng.allocate_qubit()
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
if verbose:
eng (MainEngine): the Main engine on which we run the BV algorithm
n (int): number of qubits
oracle (function): oracle used by the BV algorithm
"""
x = eng.allocate_qureg(n)
# start in uniform superposition
All(H) | x
oracle_out = eng.allocate_qubit()
X | oracle_out
H | oracle_out
oracle(eng, x, oracle_out)
All(H) | x
All(Measure) | x
Measure | oracle_out
eng.flush()
return [int(qubit) for qubit in x]
# 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]
# 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]
while i - |1>) s.t. a bit-flip turns
# into a (-1)-phase.
oracle_out = eng.allocate_qubit()
X | oracle_out
H | oracle_out
#run j iterations
with Loop(eng, j):
# oracle adds a (-1)-phase to the solution
oracle(eng, x, Dataset,threshold, 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)
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): 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)
# 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(Ph(math.pi / n)) | x
All(Measure) | x
Measure | oracle_out
eng.flush()
# return result
return [int(qubit) for qubit in x]