Ejemplo n.º 1
0
def test_swapandcnotflipper_is_available():
    flipper = _swapandcnotflipper.SwapAndCNOTFlipper(set())
    dummy = DummyEngine()
    dummy.is_available = lambda x: False
    flipper.next_engine = dummy
    eng = MainEngine(DummyEngine(save_commands=True), [])
    qubit1, qubit2 = eng.allocate_qureg(2)
    Swap | (qubit1, qubit2)
    swap_count = 0
    for cmd in eng.backend.received_commands:
        if cmd.gate == Swap:
            swap_count += 1
            assert flipper.is_available(cmd)
    assert swap_count == 1

    eng = MainEngine(DummyEngine(save_commands=True), [])
    qubit1, qubit2, qubit3 = eng.allocate_qureg(3)
    with Control(eng, qubit3):
        Swap | (qubit1, qubit2)
    swap_count = 0
    for cmd in eng.backend.received_commands:
        if cmd.gate == Swap:
            swap_count += 1
            assert not flipper.is_available(cmd)
    assert swap_count == 1
Ejemplo n.º 2
0
def test_remove_barrier():
    saving_backend = DummyEngine(save_commands=True)

    def my_is_available(cmd):
        return not cmd.gate == Barrier

    saving_backend.is_available = my_is_available
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    R(0.2) | qubit
    Barrier | qubit
    eng.flush(deallocate_qubits=True)
    # Don't test initial allocate and trailing deallocate and flush gate.
    for cmd in saving_backend.received_commands[1:-2]:
        assert not cmd.gate == Barrier
    assert len(saving_backend.received_commands[1:-2]) == 1