Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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]
def create_bell_pair(eng):
"""
Returns a Bell-pair (two qubits in state :math:`|A\rangle \otimes |B
\rangle = \frac 1{\sqrt 2} \left( |0\rangle\otimes|0\rangle + |1\rangle
\otimes|1\rangle \right)`).
Args:
eng (MainEngine): MainEngine from which to allocate the qubits.
Returns:
bell_pair (tuple): The Bell-pair.
"""
b1 = eng.allocate_qubit()
b2 = eng.allocate_qubit()
H | b1
CNOT | (b1, b2)
return b1, b2
def hgate(self):
tl = []
for nsite, num_bench in zip(NL, [1000, 1000, 1000, 100, 10, 5]):
print('========== N: %d ============'%nsite)
for func in [bG(ops.H), bCG(ops.H), bRG(ops.H)]:
tl.append(qbenchmark(func, nsite, num_bench)*1e6)
np.savetxt('h-report.dat', tl)
elif operation['name'] in ['u1']:
params = operation['params']
qubit = qureg[operation['qubits'][0]]
Rz(params[0]) | qubit
elif operation['name'] in ['u2']:
params = operation['params']
qubit = qureg[operation['qubits'][0]]
Rz(params[1] - np.pi/2) | qubit
Rx(np.pi/2) | qubit
Rz(params[0] + np.pi/2) | qubit
elif operation['name'] == 't':
qubit = qureg[operation['qubits'][0]]
T | qubit
elif operation['name'] == 'h':
qubit = qureg[operation['qubits'][0]]
H | qubit
elif operation['name'] == 's':
qubit = qureg[operation['qubits'][0]]
S | qubit
elif operation['name'] in ['CX', 'cx']:
qubit0 = qureg[operation['qubits'][0]]
qubit1 = qureg[operation['qubits'][1]]
CX | (qubit0, qubit1)
elif operation['name'] in ['id', 'u0']:
pass
# Check if measure
elif operation['name'] == 'measure':
qubit_index = operation['qubits'][0]
qubit = qureg[qubit_index]
clbit = operation['clbits'][0]
Measure | qubit
bit = 1 << clbit
def _decompose_rx(cmd):
""" Decompose the Rx gate."""
qubit = cmd.qubits[0]
eng = cmd.engine
angle = cmd.gate.angle
with Control(eng, cmd.control_qubits):
with Compute(eng):
H | qubit
Rz(angle) | qubit
Uncompute(eng)
H | target
CNOT | (c1, target)
T | c1
Tdag | target
CNOT | (c2, target)
CNOT | (c2, c1)
Tdag | c1
T | target
CNOT | (c2, c1)
CNOT | (c1, target)
Tdag | target
CNOT | (c2, target)
T | target
T | c2
H | target
"""
Runs a test circuit on the provided compiler engine.
Args:
eng (MainEngine): Main compiler engine to use.
Returns:
measurement (list): List of measurement outcomes.
"""
# allocate the quantum register to entangle
qureg = eng.allocate_qureg(16)
interactions = [(1, 0), (1, 2), (2, 3), (3, 4), (5, 4), (6, 5), (6, 7),
(8, 7), (9, 8), (9, 10), (11, 10), (12, 11), (12, 13),
(13, 14), (15, 14)]
H | qureg[0]
for e in interactions:
flip = e[0] > e[1]
if flip:
All(H) | [qureg[e[0]], qureg[e[1]]]
CNOT | (qureg[e[0]], qureg[e[1]])
All(H) | [qureg[e[0]], qureg[e[1]]]
else:
CNOT | (qureg[e[0]], qureg[e[1]])
# measure; should be all-0 or all-1
Measure | qureg
# run the circuit
eng.flush()
# access the probabilities via the back-end:
CNOT | (qubits[3], qubits[6])
CNOT | (qubits[3], qubits[9])
H | qubits[3]
CNOT | (qubits[3], qubits[4])
CNOT | (qubits[3], qubits[5])
H | qubits[6]
CNOT | (qubits[6], qubits[7])
CNOT | (qubits[6], qubits[8])
H | qubits[9]
CNOT | (qubits[9], qubits[10])
CNOT | (qubits[9], qubits[11])
H | qubits[3]
H | qubits[4]
H | qubits[5]
H | qubits[6]
H | qubits[7]
H | qubits[8]
H | qubits[9]
H | qubits[10]
H | qubits[11]
CNOT | (qubits[1], qubits[3])
CNOT | (qubits[1], qubits[4])
CNOT | (qubits[1], qubits[5])
CNOT | (qubits[1], qubits[6])
CNOT | (qubits[1], qubits[7])
CNOT | (qubits[1], qubits[8])
CNOT | (qubits[1], qubits[9])
CNOT | (qubits[1], qubits[10])
CNOT | (qubits[1], qubits[11])
H | qubits[3]
H | qubits[4]