Esempio n. 1
0
def test_auto_replacer_priorize_controlstate_rule():
    # Check that when a control state is given and it has negative control,
    # Autoreplacer prioritizes the corresponding decomposition rule before anything else.
    # (Decomposition rule should have name _decompose_controlstate)

    # Create test gate and inverse
    class ControlGate(BasicGate):
        pass

    def _decompose_controlstate(cmd):
        S | cmd.qubits

    def _decompose_random(cmd):
        H | cmd.qubits

    def control_filter(self, cmd):
        if cmd.gate == ControlGate():
            return False
        return True

    rule_set.add_decomposition_rule(
        DecompositionRule(BasicGate, _decompose_random))

    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=backend,
                     engine_list=[
                         _replacer.AutoReplacer(rule_set),
                         _replacer.InstructionFilter(control_filter)
                     ])
    assert len(backend.received_commands) == 0
    qb = eng.allocate_qubit()
    ControlGate() | qb
    eng.flush()
    assert len(backend.received_commands) == 3
    assert backend.received_commands[1].gate == H

    rule_set.add_decomposition_rule(
        DecompositionRule(BasicGate, _decompose_controlstate))

    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=backend,
                     engine_list=[
                         _replacer.AutoReplacer(rule_set),
                         _replacer.InstructionFilter(control_filter)
                     ])
    assert len(backend.received_commands) == 0
    qb = eng.allocate_qubit()
    ControlGate() | qb
    eng.flush()
    assert len(backend.received_commands) == 3
    assert backend.received_commands[1].gate == S
Esempio n. 2
0
def test_gate_filter():
    # BasicGate with no get_inverse used for testing:
    with pytest.raises(NotInvertible):
        TestGate.get_inverse()
    # Loading of decomposition rules:
    def decompose_test1(cmd):
        qb = cmd.qubits
        X | qb

    def recognize_test(cmd):
        return True

    register_decomposition(TestGate.__class__, decompose_test1, recognize_test)

    def decompose_test2(cmd):
        qb = cmd.qubits
        H | qb

    register_decomposition(TestGate.__class__, decompose_test2, recognize_test)

    assert len(decompositions[TestGate.__class__.__name__]) == 2

    # Filter which doesn't allow TestGate
    def test_gate_filter_func(self, cmd):
        if cmd.gate == TestGate:
            return False
        return True

    return _replacer.InstructionFilter(test_gate_filter_func)
Esempio n. 3
0
def fixture_gate_filter():
    # Filter which doesn't allow SomeGate
    def test_gate_filter_func(self, cmd):
        if cmd.gate == SomeGate:
            return False
        return True

    return _replacer.InstructionFilter(test_gate_filter_func)
Esempio n. 4
0
def test_filter_engine():
    def my_filter(self, cmd):
        if cmd.gate == H:
            return True
        return False

    filter_eng = _replacer.InstructionFilter(my_filter)
    eng = MainEngine(backend=DummyEngine(), engine_list=[filter_eng])
    qubit = eng.allocate_qubit()
    cmd = Command(eng, H, (qubit, ))
    cmd2 = Command(eng, X, (qubit, ))
    assert eng.is_available(cmd)
    assert not eng.is_available(cmd2)
    assert filter_eng.is_available(cmd)
    assert not filter_eng.is_available(cmd2)
Esempio n. 5
0
def test_auto_replacer_no_rule_found():
    # Check that exception is thrown if no rule is found
    # For both the cmd and it's inverse (which exists)
    def h_filter(self, cmd):
        if cmd.gate == H:
            return False
        return True

    h_filter = _replacer.InstructionFilter(h_filter)
    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=backend,
                     engine_list=[_replacer.AutoReplacer(rule_set), h_filter])
    qubit = eng.allocate_qubit()
    with pytest.raises(_replacer.NoGateDecompositionError):
        H | qubit
    eng.flush()
Esempio n. 6
0
def test_auto_replacer_use_inverse_decomposition():
    # Check that if there is no decomposition for the gate, that
    # AutoReplacer runs the decomposition for the inverse gate in reverse

    # Create test gate and inverse
    class NoMagicGate(BasicGate):
        pass

    class MagicGate(BasicGate):
        def get_inverse(self):
            return NoMagicGate()

    def decompose_no_magic_gate(cmd):
        qb = cmd.qubits
        Rx(0.6) | qb
        H | qb

    def recognize_no_magic_gate(cmd):
        return True

    rule_set.add_decomposition_rule(
        DecompositionRule(NoMagicGate, decompose_no_magic_gate,
                          recognize_no_magic_gate))

    def magic_filter(self, cmd):
        if cmd.gate == MagicGate():
            return False
        return True

    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=backend,
                     engine_list=[
                         _replacer.AutoReplacer(rule_set),
                         _replacer.InstructionFilter(magic_filter)
                     ])
    assert len(backend.received_commands) == 0
    qb = eng.allocate_qubit()
    MagicGate() | qb
    eng.flush()
    for cmd in backend.received_commands:
        print(cmd)
    assert len(backend.received_commands) == 4
    assert backend.received_commands[1].gate == H
    assert backend.received_commands[2].gate == Rx(-0.6)
Esempio n. 7
0
def test_auto_replacer_searches_parent_class_for_rule():
    class DerivedSomeGate(SomeGateClass):
        pass

    def test_gate_filter_func(self, cmd):
        if (cmd.gate == X or cmd.gate == H
                or isinstance(cmd.gate, ClassicalInstructionGate)):
            return True
        return False

    i_filter = _replacer.InstructionFilter(test_gate_filter_func)
    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=backend,
                     engine_list=[_replacer.AutoReplacer(rule_set), i_filter])
    qb = eng.allocate_qubit()
    DerivedSomeGate() | qb
    eng.flush()
    received_gate = backend.received_commands[1].gate
    assert received_gate == X or received_gate == H