예제 #1
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
def test_decomposition(gate_matrix):
    # Create single qubit gate with gate_matrix
    test_gate = BasicGate()
    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)

        Measure | test_qb + test_ctrl_qb
        Measure | correct_qb + correct_ctrl_qb
        test_eng.flush(deallocate_qubits=True)
        correct_eng.flush(deallocate_qubits=True)
예제 #3
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 = 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
    eng.flush(deallocate_qubits=True)
    for cmd in saving_backend.received_commands:
        assert not 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)
예제 #5
0
        LocalOptimizer(cache_depth),
        #,CommandPrinter(),
        GreedyScheduler()
    ]

    eng = HiQMainEngine(backend, engines)
    m = 2
    epsilon = 0.1  # the estimation algorithm successs with probability 1 - epsilon
    n_accuracy = 3  # the estimation algorithm estimates with 3 bits accuracy
    n = _bits_required_to_achieve_accuracy(n_accuracy, epsilon)

    ## we create a unitary U = R(cmath.pi*3/4) \ox R(cmath.pi*3/4)
    U = BasicGate()
    theta = math.pi * 3 / 4
    U.matrix = np.matrix([[1, 0, 0, 0], [0, cmath.exp(1j * theta), 0, 0],
                          [0, 0, cmath.exp(1j * theta), 0],
                          [0, 0, 0, cmath.exp(1j * 2 * theta)]])

    state = eng.allocate_qureg(m + n)

    # prepare the input state to be |psi>=|01>
    X | state[1]

    run_phase_estimation(eng, U, state, m, n)

    # we have to post-process the state that stores the estimated phase
    OFFSET = m
    Tensor(Measure) | state[OFFSET:]
    Tensor(Measure) | state[:OFFSET]
    eng.flush()