How to use the projectq.meta.Compute function in projectq

To help you get started, weâ€™ve selected a few projectq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

QuTech-Delft / quantuminspire / docs / example_projectq_grover.py View on Github
``````num_it = int(math.pi / 4. * math.sqrt(1 &lt;&lt; n))

# prepare the oracle output qubit (the one that is flipped to indicate the
# solution. start in state 1/sqrt(2) * (|0&gt; - |1&gt;) 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]``````
Huawei-HiQ / HiQsimulator / examples / maximum_search_constant_mpi.py View on Github
``````All(H) | x

# prepare the oracle output qubit (the one that is flipped to indicate the
# solution. start in state 1/sqrt(2) * (|0&gt; - |1&gt;) 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)

All(Measure) | x
Measure | oracle_out

k=0
xvalue=0
while k``````
Huawei-HiQ / HiQsimulator / examples / unknown_num_search_constant_mpi.py View on Github
``````All(H) | x

# prepare the oracle output qubit (the one that is flipped to indicate the
# solution. start in state 1/sqrt(2) * (|0&gt; - |1&gt;) 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)

All(Measure) | x
Measure | oracle_out

k=0
xvalue=0
while k``````
QuTech-Delft / quantuminspire / docs / example_projectq_grover.py View on Github
``````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)``````
Huawei-HiQ / HiQsimulator / examples / exactgrover_mpi.py View on Github
``````def alternating_bits_oracle(eng, qubits, output):
"""
Marks the solution string 0, 1, 0,...,0 by flipping the

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):
X | qubits[1]
with Control(eng, qubits):
X | output
Uncompute(eng)``````
ProjectQ-Framework / ProjectQ / projectq / libs / math / _constantmath.py View on Github
``````def add_constant(eng, c, quint):
"""
Adds a classical constant c to the quantum integer (qureg) quint using

Note: Uses the Fourier-transform adder from
https://arxiv.org/abs/quant-ph/0008033.
"""

with Compute(eng):
QFT | quint

for i in range(len(quint)):
for j in range(i, -1, -1):
if ((c &gt;&gt; j) &amp; 1):
R(math.pi / (1 &lt;&lt; (i - j))) | quint[i]

Uncompute(eng)``````
ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / rx2rz.py View on Github
``````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)``````
ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / swap2cnot.py View on Github
``````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)``````
ProjectQ-Framework / ProjectQ / projectq / setups / decompositions / time_evolution.py View on Github
``````# 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:
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)``````
Huawei-HiQ / HiQsimulator / examples / exactgrover_mpi.py View on Github
``````def alternating_bits_oracle_modified(eng, qubits, output, phi):
"""
Marks the solution string 0,1,0,... by applying phase gate to the output bits,
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 mark the solution by a phase gate with parameter phi.
"""
with Compute(eng):
All(X) | qubits[1::2]
with Control(eng, qubits):
Rz(phi) | output
Ph(phi/2) | output

Uncompute(eng)``````

projectq

ProjectQ - An open source software framework for quantum computing

Apache-2.0