# How to use the projectq.ops.All 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.

Huawei-HiQ / HiQsimulator / examples / BV_algorithm_constant_mpi.py View on Github
``````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]
``````
QuTech-Delft / quantuminspire / docs / example_projectq_grover.py View on Github
``````# 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]``````
ProjectQ-Framework / ProjectQ / examples / grover.py View on Github
``````# 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]``````
ProjectQ-Framework / ProjectQ / examples / ibm16.py View on Github
``````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] &gt; 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:
results = eng.backend.get_probabilities(qureg)
for state, probability in sorted(list(results.items())):
print("Measured {} with p = {}.".format(state, probability))
``````
ProjectQ-Framework / ProjectQ / projectq / cengines / _ibmcnotmapper.py View on Github
``````command.engine = self.main_engine
return command

# We'll have to add all meta tags before sending on
cmd_mod_eng = CommandModifier(cmd_mod)
cmd_mod_eng.next_engine = self.next_engine
cmd_mod_eng.main_engine = self.main_engine
# forward everything to the command modifier
forwarder_eng = ForwarderEngine(cmd_mod_eng)
cmd.engine = forwarder_eng
qubit = cmd.qubits[0]
ctrl = cmd.control_qubits
# flip the CNOT using Hadamard gates:
All(H) | (ctrl + qubit)
CNOT | (qubit, ctrl)
All(H) | (ctrl + qubit)
elif cmd.gate == Allocate:
ibm_order = [2, 1, 4, 0, 3]
cmd.tags += [QubitPlacementTag(
ibm_order[all_indices[cmd.qubits[0][0].id]])]
else:

self._cmds = []``````
Huawei-HiQ / HiQsimulator / examples / ToeplitzSimplify_constant.py View on Github
``````QFT | Qubits[0:n]

#Control rotation
Control_Rotation(eng,Qubits,dataset,n)

#inverse QFT
get_inverse(QFT) | Qubits[0:n]
for i in range(int(n/2)):
Swap | (Qubits[i],Qubits[n-i-1])

#Get the quantum output and store in classical numpy. Named Quantum
qstate=qutoclass(eng,Qubits, n, 1)
#nomalized
qsum=math.sqrt(sum(abs(x)**2 for x in qstate))
qstate=qstate/qsum
All(Measure) | Qubits
'''
Classical
'''
#classical method to get the circulent matrix, and solve the problem in classical, named Circulent
F=np.zeros((2**n,2**n))*(0+0j)
for i in range(2**n):
for j in range(2**n):
F[i,j]=(1/math.sqrt(2**n))*cmath.exp(-2*math.pi*1j*j*i/2**n)
FT=np.matrix(F).H
landa=np.zeros((2**n,2**n))*(0+0j)
for i in range(2**n):
landa[i,i]=func(i,n)
Matrix1=(FT*landa*F).real

cstate1=lg.solve(Matrix1,b).real
cnorm1=math.sqrt(sum(x**2 for x in cstate1))``````
ProjectQ-Framework / ProjectQ / examples / ibm16.py View on Github
``````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] &gt; 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:
results = eng.backend.get_probabilities(qureg)
for state, probability in sorted(list(results.items())):
print("Measured {} with p = {}.".format(state, probability))

# return one (random) measurement outcome.
return [int(q) for q in qureg]``````
ProjectQ-Framework / ProjectQ / projectq / cengines / _ibmcnotmapper.py View on Github
``````def cmd_mod(command):
command.tags = cmd.tags[:] + command.tags
command.engine = self.main_engine
return command

# We'll have to add all meta tags before sending on
cmd_mod_eng = CommandModifier(cmd_mod)
cmd_mod_eng.next_engine = self.next_engine
cmd_mod_eng.main_engine = self.main_engine
# forward everything to the command modifier
forwarder_eng = ForwarderEngine(cmd_mod_eng)
cmd.engine = forwarder_eng
qubit = cmd.qubits[0]
ctrl = cmd.control_qubits
# flip the CNOT using Hadamard gates:
All(H) | (ctrl + qubit)
CNOT | (qubit, ctrl)
All(H) | (ctrl + qubit)
elif cmd.gate == Allocate:
ibm_order = [2, 1, 4, 0, 3]
cmd.tags += [QubitPlacementTag(
ibm_order[all_indices[cmd.qubits[0][0].id]])]
else:

self._cmds = []``````
Huawei-HiQ / HiQsimulator / examples / exactgrover_mpi.py View on Github
``````# prepare the oracle output qubit (the one that is flipped to indicate the
# solution. start in state |1> s.t. a bit-flip turns into a e^(i*phi)-phase.
H | oracle_out
oracle_modified(eng, x, oracle_out, phi)

with Compute(eng):
All(H) | x
All(X) | x

with Control(eng, x[0:-1]):
Rz(varphi) | x[-1]
Ph(varphi/2) | x[-1]

Uncompute(eng)

All(Measure) | x
Measure | oracle_out

eng.flush()
# return result
return [int(qubit) for qubit in x]``````

## projectq

ProjectQ - An open source software framework for quantum computing

Apache-2.0