示例#1
0
def test_decomposition(angle):
    for basis_state in ([1, 0], [0, 1]):
        correct_dummy_eng = DummyEngine(save_commands=True)
        correct_eng = MainEngine(backend=Simulator(), engine_list=[correct_dummy_eng])

        rule_set = DecompositionRuleSet(modules=[rx2rz])
        test_dummy_eng = DummyEngine(save_commands=True)
        test_eng = MainEngine(
            backend=Simulator(),
            engine_list=[
                AutoReplacer(rule_set),
                InstructionFilter(rx_decomp_gates),
                test_dummy_eng,
            ],
        )

        correct_qb = correct_eng.allocate_qubit()
        Rx(angle) | correct_qb
        correct_eng.flush()

        test_qb = test_eng.allocate_qubit()
        Rx(angle) | test_qb
        test_eng.flush()

        assert correct_dummy_eng.received_commands[1].gate == Rx(angle)
        assert test_dummy_eng.received_commands[1].gate != Rx(angle)

        for fstate in ['0', '1']:
            test = test_eng.backend.get_amplitude(fstate, test_qb)
            correct = correct_eng.backend.get_amplitude(fstate, correct_qb)
            assert correct == pytest.approx(test, rel=1e-12, abs=1e-12)

        Measure | test_qb
        Measure | correct_qb
示例#2
0
def test_resource_counter_depth_of_dag():
    resource_counter = ResourceCounter()
    eng = MainEngine(resource_counter, [])
    assert resource_counter.depth_of_dag == 0
    qb0 = eng.allocate_qubit()
    qb1 = eng.allocate_qubit()
    qb2 = eng.allocate_qubit()
    QFT | qb0 + qb1 + qb2
    assert resource_counter.depth_of_dag == 1
    H | qb0
    H | qb0
    assert resource_counter.depth_of_dag == 3
    CNOT | (qb0, qb1)
    X | qb1
    assert resource_counter.depth_of_dag == 5
    Measure | qb1
    Measure | qb1
    assert resource_counter.depth_of_dag == 7
    CNOT | (qb1, qb2)
    Measure | qb2
    assert resource_counter.depth_of_dag == 9
    qb1[0].__del__()
    qb2[0].__del__()
    assert resource_counter.depth_of_dag == 9
    qb0[0].__del__()
    assert resource_counter.depth_of_dag == 9
示例#3
0
def test_openqasm_test_qasm_single_qubit_gates_control():
    backend = OpenQASMEngine()
    eng = MainEngine(backend=backend, engine_list=[])
    qubit = eng.allocate_qubit()
    ctrl = eng.allocate_qubit()

    with Control(eng, ctrl):
        H | qubit
        X | qubit
        Y | qubit
        Z | qubit
        NOT | qubit
        R(0.5) | qubit
        Rz(0.5) | qubit
        Ph(0.5) | qubit
    eng.flush()

    qasm = [l for l in backend.circuit.qasm().split('\n')[2:] if l]
    assert qasm == [
        'qreg q0[1];', 'qreg q1[1];', 'creg c0[1];', 'creg c1[1];',
        'ch q1[0],q0[0];', 'cx q1[0],q0[0];', 'cy q1[0],q0[0];',
        'cz q1[0],q0[0];', 'cx q1[0],q0[0];',
        'cu1(0.500000000000000) q1[0],q0[0];',
        'crz(0.500000000000000) q1[0],q0[0];',
        'cu1(-0.250000000000000) q1[0],q0[0];'
    ]
示例#4
0
def test_resource_counter():
    resource_counter = ResourceCounter()
    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend, [resource_counter])

    qubit1 = eng.allocate_qubit()
    qubit2 = eng.allocate_qubit()
    H | qubit1
    X | qubit2
    del qubit2

    qubit3 = eng.allocate_qubit()
    CNOT | (qubit1, qubit3)

    Measure | (qubit1, qubit3)

    assert int(qubit1) == int(qubit3)
    assert int(qubit1) == 0

    assert resource_counter.max_width == 2

    str_repr = str(resource_counter)
    assert str_repr.count("H") == 1
    assert str_repr.count("X") == 2
    assert str_repr.count("CX") == 1
    assert str_repr.count("Allocate : 3") == 1
    assert str_repr.count("Deallocate : 1") == 1

    sent_gates = [cmd.gate for cmd in backend.received_commands]
    assert sent_gates.count(H) == 1
    assert sent_gates.count(X) == 2
    assert sent_gates.count(Measure) == 1
def test_decomposition():
    for basis_state_index in range(0, 16):
        basis_state = [0] * 16
        basis_state[basis_state_index] = 1.0
        correct_dummy_eng = DummyEngine(save_commands=True)
        correct_eng = MainEngine(backend=Simulator(), engine_list=[correct_dummy_eng])
        rule_set = DecompositionRuleSet(modules=[cnu2toffoliandcu])
        test_dummy_eng = DummyEngine(save_commands=True)
        test_eng = MainEngine(
            backend=Simulator(),
            engine_list=[
                AutoReplacer(rule_set),
                InstructionFilter(_decomp_gates),
                test_dummy_eng,
            ],
        )
        test_sim = test_eng.backend
        correct_sim = correct_eng.backend
        correct_qb = correct_eng.allocate_qubit()
        correct_ctrl_qureg = correct_eng.allocate_qureg(3)
        correct_eng.flush()
        test_qb = test_eng.allocate_qubit()
        test_ctrl_qureg = test_eng.allocate_qureg(3)
        test_eng.flush()

        correct_sim.set_wavefunction(basis_state, correct_qb + correct_ctrl_qureg)
        test_sim.set_wavefunction(basis_state, test_qb + test_ctrl_qureg)

        with Control(test_eng, test_ctrl_qureg[:2]):
            Rx(0.4) | test_qb
        with Control(test_eng, test_ctrl_qureg):
            Ry(0.6) | test_qb
        with Control(test_eng, test_ctrl_qureg):
            X | test_qb

        with Control(correct_eng, correct_ctrl_qureg[:2]):
            Rx(0.4) | correct_qb
        with Control(correct_eng, correct_ctrl_qureg):
            Ry(0.6) | correct_qb
        with Control(correct_eng, correct_ctrl_qureg):
            X | correct_qb

        test_eng.flush()
        correct_eng.flush()

        assert len(correct_dummy_eng.received_commands) == 9
        assert len(test_dummy_eng.received_commands) == 25

        for fstate in range(16):
            binary_state = format(fstate, '04b')
            test = test_sim.get_amplitude(binary_state, test_qb + test_ctrl_qureg)
            correct = correct_sim.get_amplitude(binary_state, correct_qb + correct_ctrl_qureg)
            assert correct == pytest.approx(test, rel=1e-12, abs=1e-12)

        All(Measure) | test_qb + test_ctrl_qureg
        All(Measure) | correct_qb + correct_ctrl_qureg
        test_eng.flush(deallocate_qubits=True)
        correct_eng.flush(deallocate_qubits=True)
示例#6
0
def test_openqasm_is_available(gate, is_available):
    eng = MainEngine(backend=DummyEngine(), engine_list=[OpenQASMEngine()])
    qubit1 = eng.allocate_qubit()
    cmd = Command(eng, gate, (qubit1, ))
    eng.is_available(cmd) == is_available

    eng = MainEngine(backend=OpenQASMEngine(), engine_list=[])
    qubit1 = eng.allocate_qubit()
    cmd = Command(eng, gate, (qubit1, ))
    eng.is_available(cmd) == is_available
示例#7
0
def test_decomposition(gate_matrix):
    # Create single qubit gate with gate_matrix
    test_gate = MatrixGate()
    test_gate.matrix = np.matrix(gate_matrix)

    for basis_state in ([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0,
                                                                   1]):
        correct_dummy_eng = DummyEngine(save_commands=True)
        correct_eng = MainEngine(backend=Simulator(),
                                 engine_list=[correct_dummy_eng])

        rule_set = DecompositionRuleSet(modules=[carb1q])
        test_dummy_eng = DummyEngine(save_commands=True)
        test_eng = MainEngine(
            backend=Simulator(),
            engine_list=[
                AutoReplacer(rule_set),
                InstructionFilter(_decomp_gates),
                test_dummy_eng,
            ],
        )
        test_sim = test_eng.backend
        correct_sim = correct_eng.backend

        correct_qb = correct_eng.allocate_qubit()
        correct_ctrl_qb = correct_eng.allocate_qubit()
        correct_eng.flush()
        test_qb = test_eng.allocate_qubit()
        test_ctrl_qb = test_eng.allocate_qubit()
        test_eng.flush()

        correct_sim.set_wavefunction(basis_state, correct_qb + correct_ctrl_qb)
        test_sim.set_wavefunction(basis_state, test_qb + test_ctrl_qb)

        with Control(test_eng, test_ctrl_qb):
            test_gate | test_qb
        with Control(correct_eng, correct_ctrl_qb):
            test_gate | correct_qb

        test_eng.flush()
        correct_eng.flush()

        assert correct_dummy_eng.received_commands[3].gate == test_gate
        assert test_dummy_eng.received_commands[3].gate != test_gate

        for fstate in ['00', '01', '10', '11']:
            test = test_sim.get_amplitude(fstate, test_qb + test_ctrl_qb)
            correct = correct_sim.get_amplitude(fstate,
                                                correct_qb + correct_ctrl_qb)
            assert correct == pytest.approx(test, rel=1e-12, abs=1e-12)

        All(Measure) | test_qb + test_ctrl_qb
        All(Measure) | correct_qb + correct_ctrl_qb
        test_eng.flush(deallocate_qubits=True)
        correct_eng.flush(deallocate_qubits=True)
示例#8
0
def test_recognize_correct_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    ctrl_qubit = eng.allocate_qubit()
    eng.flush()
    Rx(0.3) | qubit
    with Control(eng, ctrl_qubit):
        Rx(0.4) | qubit
    eng.flush(deallocate_qubits=True)
    assert rx2rz._recognize_RxNoCtrl(saving_backend.received_commands[3])
    assert not rx2rz._recognize_RxNoCtrl(saving_backend.received_commands[4])
示例#9
0
def test_openqasm_is_available_2control(gate, is_available):
    eng = MainEngine(backend=DummyEngine(), engine_list=[OpenQASMEngine()])
    qubit1 = eng.allocate_qubit()
    qureg = eng.allocate_qureg(2)
    cmd = Command(eng, gate, (qubit1, ), controls=qureg)
    assert eng.is_available(cmd) == is_available

    eng = MainEngine(backend=OpenQASMEngine(), engine_list=[])
    qubit1 = eng.allocate_qubit()
    qureg = eng.allocate_qureg(2)
    cmd = Command(eng, gate, (qubit1, ), controls=qureg)
    assert eng.is_available(cmd) == is_available
def test_recognize_incorrect_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    ctrl_qubit = eng.allocate_qubit()
    ctrl_qureg = eng.allocate_qureg(2)
    eng.flush()
    with Control(eng, ctrl_qubit):
        Rx(0.3) | qubit
    X | qubit
    with Control(eng, ctrl_qureg):
        X | qubit
    eng.flush(deallocate_qubits=True)
    for cmd in saving_backend.received_commands:
        assert not cnu2toffoliandcu._recognize_CnU(cmd)
示例#11
0
def test_cannot_allocate_past_max():
    mapper = BoundedQubitMapper(1)
    engine = MainEngine(
        DummyEngine(),
        engine_list=[mapper],
        verbose=True,
    )
    engine.allocate_qubit()
    with pytest.raises(RuntimeError) as excinfo:
        engine.allocate_qubit()

    assert str(excinfo.value) == "Cannot allocate more than 1 qubits!"

    # Avoid double error reporting
    mapper.current_mapping = {0: 0, 1: 1}
示例#12
0
def test_ibm_backend_is_available_control_not(num_ctrl_qubits, is_available):
    eng = MainEngine(backend=DummyEngine(), engine_list=[DummyEngine()])
    qubit1 = eng.allocate_qubit()
    qureg = eng.allocate_qureg(num_ctrl_qubits)
    ibm_backend = _ibm.IBMBackend()
    cmd = Command(eng, NOT, (qubit1,), controls=qureg)
    assert ibm_backend.is_available(cmd) == is_available
def test_unitary_not_last_engine():
    eng = MainEngine(backend=DummyEngine(save_commands=True), engine_list=[UnitarySimulator()])
    qubit = eng.allocate_qubit()
    X | qubit
    eng.flush()
    Measure | qubit
    assert len(eng.backend.received_commands) == 4
示例#14
0
def test_openqasm_test_qasm_single_qubit_gates_controls():
    backend = OpenQASMEngine()
    eng = MainEngine(backend=backend, engine_list=[], verbose=True)
    qubit = eng.allocate_qubit()
    ctrls = eng.allocate_qureg(2)

    with Control(eng, ctrls):
        X | qubit
        NOT | qubit
    eng.flush()

    qasm = [l for l in backend.circuit.qasm().split('\n')[2:] if l]

    assert qasm == [
        'qreg q0[1];',
        'qreg q1[1];',
        'qreg q2[1];',
        'creg c0[1];',
        'creg c1[1];',
        'creg c2[1];',
        'ccx q1[0],q2[0],q0[0];',
        'ccx q1[0],q2[0],q0[0];',
    ]

    with pytest.raises(RuntimeError):
        with Control(eng, ctrls):
            Y | qubit
        eng.flush()
示例#15
0
def test_openqasm_test_qasm_single_qubit_gates():
    backend = OpenQASMEngine()
    eng = MainEngine(backend=backend, engine_list=[])
    qubit = eng.allocate_qubit()

    H | qubit
    S | qubit
    T | qubit
    Sdag | qubit
    Tdag | qubit
    X | qubit
    Y | qubit
    Z | qubit
    R(0.5) | qubit
    Rx(0.5) | qubit
    Ry(0.5) | qubit
    Rz(0.5) | qubit
    Ph(0.5) | qubit
    NOT | qubit
    Measure | qubit
    eng.flush()

    qasm = [l for l in backend.circuit.qasm().split('\n')[2:] if l]
    assert qasm == [
        'qreg q0[1];', 'creg c0[1];', 'h q0[0];', 's q0[0];', 't q0[0];',
        'sdg q0[0];', 'tdg q0[0];', 'x q0[0];', 'y q0[0];', 'z q0[0];',
        'u1(0.500000000000000) q0[0];', 'rx(0.500000000000000) q0[0];',
        'ry(0.500000000000000) q0[0];', 'rz(0.500000000000000) q0[0];',
        'u1(-0.250000000000000) q0[0];', 'x q0[0];', 'measure q0[0] -> c0[0];'
    ]
def test_unitary_warnings():
    eng = MainEngine(backend=DummyEngine(save_commands=True), engine_list=[UnitarySimulator()])
    qubit = eng.allocate_qubit()
    X | qubit

    with pytest.raises(RuntimeError):
        Measure | qubit
def test_recognize_correct_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    ctrl_qubit = eng.allocate_qubit()
    eng.flush()
    with Control(eng, ctrl_qubit):
        Ph(0.1) | qubit
        R(0.2) | qubit
        Rx(0.3) | qubit
        X | qubit
    eng.flush(deallocate_qubits=True)
    # Don't test initial two allocate and flush and trailing deallocate
    # and flush gate.
    for cmd in saving_backend.received_commands[3:-3]:
        assert carb1q._recognize_carb1qubit(cmd)
示例#18
0
def test_resource_counter():
    resource_counter = ResourceCounter()
    backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend, [resource_counter])

    qubit1 = eng.allocate_qubit()
    qubit2 = eng.allocate_qubit()
    H | qubit1
    X | qubit2
    del qubit2

    qubit3 = eng.allocate_qubit()
    CNOT | (qubit1, qubit3)
    Rz(0.1) | qubit1
    Rz(0.3) | qubit1
    Rzz(0.5) | qubit1

    All(Measure) | qubit1 + qubit3

    with pytest.raises(NotYetMeasuredError):
        int(qubit1)

    assert resource_counter.max_width == 2
    assert resource_counter.depth_of_dag == 6

    str_repr = str(resource_counter)
    assert str_repr.count(" HGate : 1") == 1
    assert str_repr.count(" XGate : 1") == 1
    assert str_repr.count(" CXGate : 1") == 1
    assert str_repr.count(" Rz : 2") == 1
    assert str_repr.count(" AllocateQubitGate : 3") == 1
    assert str_repr.count(" DeallocateQubitGate : 1") == 1

    assert str_repr.count(" H : 1") == 1
    assert str_repr.count(" X : 1") == 1
    assert str_repr.count(" CX : 1") == 1
    assert str_repr.count(" Rz(0.1) : 1") == 1
    assert str_repr.count(" Rz(0.3) : 1") == 1
    assert str_repr.count(" Allocate : 3") == 1
    assert str_repr.count(" Deallocate : 1") == 1

    sent_gates = [cmd.gate for cmd in backend.received_commands]
    assert sent_gates.count(H) == 1
    assert sent_gates.count(X) == 2
    assert sent_gates.count(Measure) == 2
def test_complex_aa():
    rule_set = DecompositionRuleSet(modules=[aa])

    eng = MainEngine(
        backend=Simulator(),
        engine_list=[
            AutoReplacer(rule_set),
        ],
    )

    system_qubits = eng.allocate_qureg(6)

    # Prepare the control qubit in the |-> state
    control = eng.allocate_qubit()
    X | control
    H | control

    # Creates the initial state form the Algorithm
    complex_algorithm(eng, system_qubits)

    # Get the probabilty of getting the marked state before the AA
    # to calculate the number of iterations
    eng.flush()
    prob000000 = eng.backend.get_probability('000000', system_qubits)
    prob111111 = eng.backend.get_probability('111111', system_qubits)

    total_amp_before = math.sqrt(prob000000 + prob111111)
    theta_before = math.asin(total_amp_before)

    # Apply Quantum Amplitude Amplification the correct number of times
    # Theta is calculated previously using get_probability
    # We calculate also the theoretical final probability
    # of getting the good state
    num_it = int(math.pi / (4.0 * theta_before) + 1)
    theoretical_prob = math.sin((2 * num_it + 1.0) * theta_before)**2
    with Loop(eng, num_it):
        QAA(complex_algorithm, complex_oracle) | (system_qubits, control)

    # Get the probabilty of getting the marked state after the AA
    # to compare with the theoretical probability after the AA
    eng.flush()
    prob000000 = eng.backend.get_probability('000000', system_qubits)
    prob111111 = eng.backend.get_probability('111111', system_qubits)
    total_prob_after = prob000000 + prob111111

    All(Measure) | system_qubits
    H | control
    Measure | control

    eng.flush()

    assert total_prob_after == pytest.approx(
        theoretical_prob, abs=1e-2
    ), "The obtained probability is less than expected %f vs. %f" % (
        total_prob_after,
        theoretical_prob,
    )
示例#20
0
def test_recognize_incorrect_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    # Does not have matrix attribute:
    BasicGate() | qubit
    # Two qubit gate:
    two_qubit_gate = MatrixGate()
    two_qubit_gate.matrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                             [0, 0, 0, 1]]
    two_qubit_gate | qubit
    # Controlled single qubit gate:
    ctrl_qubit = eng.allocate_qubit()
    with Control(eng, ctrl_qubit):
        Rz(0.1) | qubit
    eng.flush(deallocate_qubits=True)
    for cmd in saving_backend.received_commands:
        assert not arb1q._recognize_arb1qubit(cmd)
示例#21
0
def test_decomposition(gate_matrix):
    for basis_state in ([1, 0], [0, 1]):
        # Create single qubit gate with gate_matrix
        test_gate = MatrixGate()
        test_gate.matrix = np.matrix(gate_matrix)

        correct_dummy_eng = DummyEngine(save_commands=True)
        correct_eng = MainEngine(backend=Simulator(),
                                 engine_list=[correct_dummy_eng])

        rule_set = DecompositionRuleSet(modules=[arb1q])
        test_dummy_eng = DummyEngine(save_commands=True)
        test_eng = MainEngine(backend=Simulator(),
                              engine_list=[
                                  AutoReplacer(rule_set),
                                  InstructionFilter(z_y_decomp_gates),
                                  test_dummy_eng
                              ])

        correct_qb = correct_eng.allocate_qubit()
        correct_eng.flush()
        test_qb = test_eng.allocate_qubit()
        test_eng.flush()

        correct_eng.backend.set_wavefunction(basis_state, correct_qb)
        test_eng.backend.set_wavefunction(basis_state, test_qb)

        test_gate | test_qb
        test_gate | correct_qb

        test_eng.flush()
        correct_eng.flush()

        assert correct_dummy_eng.received_commands[2].gate == test_gate
        assert test_dummy_eng.received_commands[2].gate != test_gate

        for fstate in ['0', '1']:
            test = test_eng.backend.get_amplitude(fstate, test_qb)
            correct = correct_eng.backend.get_amplitude(fstate, correct_qb)
            assert correct == pytest.approx(test, rel=1e-12, abs=1e-12)

        Measure | test_qb
        Measure | correct_qb
示例#22
0
def test_decomposition_errors(gate_matrix):
    test_gate = BasicGate()
    test_gate.matrix = np.matrix(gate_matrix)
    rule_set = DecompositionRuleSet(modules=[arb1q])
    eng = MainEngine(backend=DummyEngine(),
                     engine_list=[AutoReplacer(rule_set),
                                  InstructionFilter(z_y_decomp_gates)])
    qb = eng.allocate_qubit()
    with pytest.raises(Exception):
        test_gate | qb
示例#23
0
def test_recognize_correct_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    Ph(0.1) | qubit
    R(0.2) | qubit
    Rx(0.3) | qubit
    X | 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 arb1q._recognize_arb1qubit(cmd)
def test_recognize_incorrect_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    ctrl_qubit = eng.allocate_qubit()
    ctrl_qureg = eng.allocate_qureg(2)
    eng.flush()
    with Control(eng, ctrl_qubit):
        # Does not have matrix attribute:
        BasicGate() | qubit
        # Two qubit gate:
        two_qubit_gate = BasicGate()
        two_qubit_gate.matrix = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                                 [0, 0, 0, 1]]
        two_qubit_gate | qubit
    with Control(eng, ctrl_qureg):
        # Too many Control qubits:
        Rx(0.3) | qubit
    eng.flush(deallocate_qubits=True)
    for cmd in saving_backend.received_commands:
        assert not carb1q._recognize_carb1qubit(cmd)
示例#25
0
def test_chooser_Ry_reducer_synthetic():
    backend = DummyEngine(save_commands=True)
    rule_set = DecompositionRuleSet(
        modules=[projectq.libs.math, projectq.setups.decompositions])

    engine_list = [
        AutoReplacer(rule_set, chooser_Ry_reducer),
        TagRemover(),
        InstructionFilter(filter_gates),
    ]

    eng = MainEngine(backend=backend, engine_list=engine_list)
    control = eng.allocate_qubit()
    target = eng.allocate_qubit()
    CNOT | (control, target)
    CNOT | (control, target)
    eng.flush()
    idx0 = len(backend.received_commands) - 2
    idx1 = len(backend.received_commands)
    CNOT | (control, target)
    eng.flush()

    assert isinstance(backend.received_commands[idx0].gate, Ry)
    assert isinstance(backend.received_commands[idx1].gate, Ry)
    assert (backend.received_commands[idx0].gate.get_inverse() ==
            backend.received_commands[idx1].gate)

    eng = MainEngine(backend=backend, engine_list=engine_list)
    control = eng.allocate_qubit()
    target = eng.allocate_qubit()
    H | target
    eng.flush()
    idx0 = len(backend.received_commands) - 2
    idx1 = len(backend.received_commands)
    H | target
    eng.flush()

    assert isinstance(backend.received_commands[idx0].gate, Ry)
    assert isinstance(backend.received_commands[idx1].gate, Ry)
    assert (backend.received_commands[idx0].gate.get_inverse() ==
            backend.received_commands[idx1].gate)

    eng = MainEngine(backend=backend, engine_list=engine_list)
    control = eng.allocate_qubit()
    target = eng.allocate_qubit()
    Rz(1.23456) | target
    eng.flush()
    idx0 = len(backend.received_commands) - 2
    idx1 = len(backend.received_commands)
    Rz(1.23456) | target
    eng.flush()

    assert isinstance(backend.received_commands[idx0].gate, Ry)
    assert isinstance(backend.received_commands[idx1].gate, Ry)
    assert (backend.received_commands[idx0].gate.get_inverse() ==
            backend.received_commands[idx1].gate)
示例#26
0
def test_ibm_sent_error_2(monkeypatch):
    backend = _ibm.IBMBackend(verbose=True)
    mapper = BasicMapperEngine()
    res = {}
    for i in range(4):
        res[i] = i
    mapper.current_mapping = res
    eng = MainEngine(backend=backend, engine_list=[mapper])
    qubit = eng.allocate_qubit()
    Rx(math.pi) | qubit

    with pytest.raises(Exception):
        S | qubit  # no setup to decompose S gate, so not accepted by the backend
    dummy = DummyEngine()
    dummy.is_last_engine = True
    eng.next_engine = dummy
示例#27
0
def test_globalphase():
	dummy = DummyEngine(save_commands=True)
	eng = MainEngine(dummy, [AutoReplacer(),
	                       InstructionFilter(low_level_gates_noglobalphase)])
	
	qubit = eng.allocate_qubit()
	R(1.2) | qubit
	
	rz_count = 0
	for cmd in dummy.received_commands:
		assert not isinstance(cmd.gate, R)
		if isinstance(cmd.gate, Rz):
			rz_count += 1
			assert cmd.gate == Rz(1.2)
	
	assert rz_count == 1
def test_recognize_correct_gates():
    saving_backend = DummyEngine(save_commands=True)
    eng = MainEngine(backend=saving_backend)
    qubit = eng.allocate_qubit()
    ctrl_qureg = eng.allocate_qureg(2)
    ctrl_qureg2 = eng.allocate_qureg(3)
    eng.flush()
    with Control(eng, ctrl_qureg):
        Ph(0.1) | qubit
        Ry(0.2) | qubit
    with Control(eng, ctrl_qureg2):
        QFT | qubit + ctrl_qureg
        X | qubit
    eng.flush()  # To make sure gates arrive before deallocate gates
    eng.flush(deallocate_qubits=True)
    # Don't test initial 6 allocate and flush and trailing deallocate
    # and two flush gates.
    for cmd in saving_backend.received_commands[7:-8]:
        assert cnu2toffoliandcu._recognize_CnU(cmd)
示例#29
0
def test_cannot_reallocate_same_qubit():
    engine = MainEngine(
        Simulator(),
        engine_list=[BoundedQubitMapper(1)],
        verbose=True,
    )
    qureg = engine.allocate_qubit()
    qubit = qureg[0]
    qubit_id = qubit.id
    with pytest.raises(RuntimeError) as excinfo:
        allocate_cmd = Command(
            engine=engine,
            gate=AllocateQubitGate(),
            qubits=([WeakQubitRef(engine=engine, idx=qubit_id)], ),
            tags=[LogicalQubitIDTag(qubit_id)],
        )
        engine.send([allocate_cmd])

    assert str(excinfo.value) == "Qubit with id 0 has already been allocated!"
示例#30
0
def test_chooser_Ry_reducer_unsupported_gate():
    backend = DummyEngine(save_commands=True)
    rule_set = DecompositionRuleSet(
        rules=[DecompositionRule(H.__class__, _dummy_h2nothing_A)])

    engine_list = [
        AutoReplacer(rule_set, chooser_Ry_reducer),
        TagRemover(),
        InstructionFilter(filter_gates),
    ]

    eng = MainEngine(backend=backend, engine_list=engine_list)
    qubit = eng.allocate_qubit()
    H | qubit
    eng.flush()

    for cmd in backend.received_commands:
        print(cmd)

    assert isinstance(backend.received_commands[1].gate, Ry)