Exemple #1
0
# For small increments, the quadratic construction is shorter.
decompose_increment_into_cnot_triangle = DecompositionRule(
    gate_class=IncrementGate,
    gate_recognizer=max_register_sizes(8) & max_controls(3),
    gate_decomposer=lambda cmd: do_naive_increment(
        target_reg=cmd.qubits[0],
        controls=cmd.control_qubits))

# When there's lots of workspace, use double-subtract with inversion.
decompose_increment_high_workspace = DecompositionRule(
    gate_class=IncrementGate,
    gate_recognizer=max_controls(0) & min_workspace_vs_reg1(factor=1),
    gate_decomposer=lambda cmd: do_increment_with_no_controls_and_n_dirty(
        target_reg=cmd.qubits[0],
        dirty_reg=workspace(cmd)))

# When there's not a lot of workspace, do more complicated stuff.
decompose_increment_with_low_workspace = DecompositionRule(
    gate_class=IncrementGate,
    gate_recognizer=min_workspace(1),
    gate_decomposer=lambda cmd: do_increment_with_1_dirty(
        target_reg=cmd.qubits[0],
        dirty_qubit=workspace(cmd)[0],
        controls=cmd.control_qubits))

all_defined_decomposition_rules = [
    decompose_increment_into_cnot_triangle,
    decompose_increment_high_workspace,
    decompose_increment_with_low_workspace,
]
Exemple #2
0
        offset >>= 1
        jump += 1

    OffsetGate(offset) & controls | target_reg[jump:]


decompose_decrease_size = DecompositionRule(
    gate_class=OffsetGate,
    gate_recognizer=lambda cmd: cmd.gate.offset & 1 == 0,
    gate_decomposer=lambda cmd: do_decrease_size(offset=cmd.gate.offset,
                                                 target_reg=cmd.qubits[0],
                                                 controls=cmd.control_qubits))

decompose_remove_controls = DecompositionRule(
    gate_class=OffsetGate,
    gate_recognizer=min_controls(1) & min_workspace(1),
    gate_decomposer=lambda cmd: do_controlled_offset(
        gate=cmd.gate,
        target_reg=cmd.qubits[0],
        controls=cmd.control_qubits,
        dirty_qubit=workspace(cmd)[0]))

decompose_into_range_increments = DecompositionRule(
    gate_class=OffsetGate,
    gate_recognizer=max_controls(0) & _better_to_use_bitrange_offset,
    gate_decomposer=lambda cmd: do_bitrange_offset(offset=cmd.gate.offset,
                                                   target_reg=cmd.qubits[0]))

decompose_into_recursion = DecompositionRule(
    gate_class=OffsetGate,
    gate_recognizer=max_controls(0) & min_workspace(1),
Exemple #3
0
decompose_multi_not_into_cnots = DecompositionRule(
    gate_class=MultiNotGate,
    gate_decomposer=lambda cmd: do_multi_not_with_one_big_not_and_friends(
        targets=cmd.qubits[0], controls=cmd.control_qubits))

# Use many dirty bits created by other decompositions to cut all the way down
# to Toffolis.
decompose_cnot_with_big_workspace = DecompositionRule(
    gate_class=XGate,
    gate_recognizer=min_controls(3)
    & min_workspace_vs_controls(factor=1, offset=-2),
    gate_decomposer=lambda cmd: cut_not_max_controls_into_toffolis(
        target=cmd.qubits[0],
        controls=cmd.control_qubits,
        dirty_reg=workspace(cmd)))

# Use a dirty bit to cut control counts in half.
decompose_halve_cnot_with_single_workspace = DecompositionRule(
    gate_class=XGate,
    gate_recognizer=min_controls(3) & min_workspace(1),
    gate_decomposer=lambda cmd: cut_not_max_controls_in_half(
        target=cmd.qubits[0],
        controls=cmd.control_qubits,
        dirty=workspace(cmd)[0]))

all_defined_decomposition_rules = [
    decompose_multi_not_into_cnots,
    decompose_cnot_with_big_workspace,
    decompose_halve_cnot_with_single_workspace,
]