Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with Control(eng, cmd.control_qubits):
with Compute(eng):
H | qubit
Rz(angle) | qubit
Uncompute(eng)
def _recognize_RxNoCtrl(cmd):
""" For efficiency reasons only if no control qubits."""
return get_control_count(cmd) == 0
#: Decomposition rules
all_defined_decomposition_rules = [
DecompositionRule(Rx, _decompose_rx, _recognize_RxNoCtrl)
]
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)
rule_commuting_terms = DecompositionRule(
gate_class=TimeEvolution,
gate_decomposer=_decompose_time_evolution_commuting_terms,
gate_recognizer=_recognize_time_evolution_commuting_terms)
rule_individual_terms = DecompositionRule(
gate_class=TimeEvolution,
gate_decomposer=_decompose_time_evolution_individual_terms,
gate_recognizer=_recognize_time_evolution_individual_terms)
#: Decomposition rules
all_defined_decomposition_rules = [rule_commuting_terms,
rule_individual_terms]
def _decompose_R(cmd):
""" Decompose the (controlled) phase-shift gate, denoted by R(phase). """
ctrl = cmd.control_qubits
eng = cmd.engine
gate = cmd.gate
with Control(eng, ctrl):
Ph(.5 * gate.angle) | cmd.qubits
Rz(gate.angle) | cmd.qubits
#: Decomposition rules
all_defined_decomposition_rules = [
DecompositionRule(R, _decompose_R)
]
from projectq.ops import BarrierGate
def _decompose_barrier(cmd):
""" Throw out all barriers if they are not supported. """
pass
def _recognize_barrier(cmd):
""" Recognize all barriers. """
return True
#: Decomposition rules
all_defined_decomposition_rules = [
DecompositionRule(BarrierGate, _decompose_barrier, _recognize_barrier)
]
qubit_graph(Graph): Graph object specifying connectivity of qubits.
The values of the nodes of this unique qubit ids. If None,
all-to-all connectivity is assumed.
opt_size(int): Number for ProjectQ local optimizer to determine size
of blocks optimized over.
Returns:
projectq.cengine that is the compiler engine set up with these
rules and decompostions.
"""
rule_set = (
projectq.cengines.
DecompositionRuleSet(modules=[projectq.setups.decompositions]))
# Set rules for splitting non-commuting operators
trotter_rule_set = (projectq.cengines.DecompositionRule(
gate_class=TimeEvolution,
gate_decomposer=_first_order_trotter,
gate_recognizer=_identify_non_commuting))
rule_set.add_decomposition_rule(trotter_rule_set)
# Set rules for 2 qubit gates that act on non-adjacent qubits
if qubit_graph is not None:
connectivity_rule_set = (
projectq.cengines.DecompositionRule(
gate_class=projectq.ops.NOT.__class__,
gate_decomposer=(lambda x: _direct_graph_swap(x, qubit_graph)),
gate_recognizer=(lambda x: _non_adjacent_filter(None, x,
qubit_graph,
True))))
rule_set.add_decomposition_rule(connectivity_rule_set)
with Control(eng, cmd.control_qubits):
add_constant_modN(eng, c, N, quint)
def _replace_multiplybyconstantmodN(cmd):
eng = cmd.engine
c = cmd.gate.a
N = cmd.gate.N
quint = cmd.qubits[0]
with Control(eng, cmd.control_qubits):
mul_by_constant_modN(eng, c, N, quint)
all_defined_decomposition_rules = [
DecompositionRule(AddConstant, _replace_addconstant),
DecompositionRule(AddConstantModN, _replace_addconstmodN),
DecompositionRule(MultiplyByConstantModN, _replace_multiplybyconstantmodN),
]
a, b_half, c_half, d_half = _find_parameters(matrix)
qb = cmd.qubits
eng = cmd.engine
with Control(eng, cmd.control_qubits):
if Rz(2*d_half) != Rz(0):
Rz(2*d_half) | qb
if Ry(2*c_half) != Ry(0):
Ry(2*c_half) | qb
if Rz(2*b_half) != Rz(0):
Rz(2*b_half) | qb
if a != 0:
Ph(a) | qb
all_defined_decomposition_rules = [
DecompositionRule(BasicGate, _decompose_arb1qubit, _recognize_arb1qubit)
]
n = get_control_count(cmd)
Rz(0.5 * gate.angle) | qubit
C(NOT, n) | (ctrl, qubit)
Rz(-0.5 * gate.angle) | qubit
C(NOT, n) | (ctrl, qubit)
def _recognize_CRz(cmd):
""" Recognize the controlled Rz gate. """
return get_control_count(cmd) >= 1
#: Decomposition rules
all_defined_decomposition_rules = [
DecompositionRule(Rz, _decompose_CRz, _recognize_CRz)
]
add_constant_modN(eng, c, N, quint)
def _replace_multiplybyconstantmodN(cmd):
eng = cmd.engine
c = cmd.gate.a
N = cmd.gate.N
quint = cmd.qubits[0]
with Control(eng, cmd.control_qubits):
mul_by_constant_modN(eng, c, N, quint)
all_defined_decomposition_rules = [
DecompositionRule(AddConstant, _replace_addconstant),
DecompositionRule(AddConstantModN, _replace_addconstmodN),
DecompositionRule(MultiplyByConstantModN, _replace_multiplybyconstantmodN),
]
if Ry(-c_half) != Ry(0):
Ry(-c_half) | qb
with Control(eng, cmd.control_qubits):
X | qb
if Ry(c_half) != Ry(0):
Ry(c_half) | qb
if Rz(b) != Rz(0):
Rz(b) | qb
if a != 0:
with Control(eng, cmd.control_qubits):
Ph(a) | qb
#: Decomposition rules
all_defined_decomposition_rules = [
DecompositionRule(BasicGate, _decompose_carb1qubit, _recognize_carb1qubit)
]