Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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:
p = fun.tolist().index(1)
fun[p] = 0
i=0
a=a-1
while p/2 != 0:
num[i] = p % 2
p = p//2
i = i+1
a1 = sum(num)
num1=num
while a1>0:
p = num1.index(1)
a1 = a1-1
num1[p]=0
X | x[p]
with Control(eng, x):
X | output
a1=sum(num)
while a1>0:
p = num.index(1)
a1 = a1-1
num[p]=0
X | x[p]
def _decompose_swap(cmd):
""" Decompose (controlled) swap gates. """
ctrl = cmd.control_qubits
eng = cmd.engine
with Compute(eng):
CNOT | (cmd.qubits[0], cmd.qubits[1])
with Control(eng, ctrl):
CNOT | (cmd.qubits[1], cmd.qubits[0])
Uncompute(eng)
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, 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>")
p = fun.tolist().index(1)
fun[p] = 0
i=0
a=a-1
while p/2 != 0:
num[i] = p % 2
p = p//2
i = i+1
a1 = sum(num)
num1=num
while a1>0:
p = num1.index(1)
a1 = a1-1
num1[p]=0
X | x[p]
with Control(eng, x):
X | output
a1=sum(num)
while a1>0:
p = num.index(1)
a1 = a1-1
num[p]=0
X | x[p]
state_creation_function(eng, psi)
# entangle it with Alice's b1
CNOT | (psi, b1)
print("= Step 2. Alice entangles the state 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)]
print("= Step 3. Alice sends the classical 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
print("= Step 4. Bob tries to recover the state created by Alice")
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()
print("\t Bob successfully arrived at |0>")
"""
Multiplies a quantum integer by a classical number a modulo N, i.e.,
|x> -> |a*x mod N>
(only works if a and N are relative primes, otherwise the modular inverse
does not exist).
"""
assert(c < N and c >= 0)
assert(gcd(c, N) == 1)
n = len(quint_in)
quint_out = eng.allocate_qureg(n + 1)
for i in range(n):
with Control(eng, quint_in[i]):
AddConstantModN((c << i) % N, N) | quint_out
for i in range(n):
Swap | (quint_out[i], quint_in[i])
cinv = inv_mod_N(c, N)
for i in range(n):
with Control(eng, quint_in[i]):
SubConstantModN((cinv << i) % N, N) | quint_out
del quint_out
# Check that hamiltonian is not identity term,
# Previous __or__ operator should have apply a global phase instead:
assert not term == ()
# hamiltonian has only a single local operator
if len(term) == 1:
with Control(eng, cmd.control_qubits):
if term[0][1] == 'X':
Rx(time * coefficient * 2.) | qureg[term[0][0]]
elif term[0][1] == 'Y':
Ry(time * coefficient * 2.) | qureg[term[0][0]]
else:
Rz(time * coefficient * 2.) | qureg[term[0][0]]
# hamiltonian has more than one local operator
else:
with Control(eng, cmd.control_qubits):
with Compute(eng):
# Apply local basis rotations
for index, action in term:
check_indices.add(index)
if action == 'X':
H | qureg[index]
elif action == 'Y':
Rx(math.pi / 2.) | qureg[index]
# Check that qureg had exactly as many qubits as indices:
assert check_indices == set((range(len(qureg))))
# Compute parity
for i in range(len(qureg)-1):
CNOT | (qureg[i], qureg[i+1])
Rz(time * coefficient * 2.) | qureg[-1]
# Uncompute parity and basis change
Uncompute(eng)