Beispiel #1
0
    def test_submit_job(self):

        # Create a valid qpu
        qpu_valid = SimulatedAnnealing(
            temp_t=TestSimulatedAnnealing.temp_t_valid,
            n_steps=TestSimulatedAnnealing.n_steps_valid,
            seed=8017)

        # Create an Observable Job and check that such Jobs are not dealt with by the qpu
        observable = Observable(5)
        job = Job(observable=observable)
        with pytest.raises(exceptions_types.QPUException):
            assert result == qpu_valid.submit_job(job)

        # Create a circuit Job a and check that such Jobs are not dealt with by the qpu
        from qat.lang.AQASM import Program, H
        prog = Program()
        reg = prog.qalloc(1)
        prog.apply(H, reg)
        prog.reset(reg)
        with pytest.raises(exceptions_types.QPUException):
            assert result == qpu_valid.submit(prog.to_circ().to_job(nbshots=1))

        # Create a Job from a Schedule with empty drive and check that such
        # Jobs are not dealt with by the qpu
        schedule = Schedule()
        job = Job(schedule=schedule)
        with pytest.raises(exceptions_types.QPUException):
            assert result == qpu_valid.submit_job(job)

        # Create a job from a Schedule with a drive with more than one observable
        # or an observable with coefficient not 1 to check that such Jobs don't work
        # with the qpu
        observable = get_observable(TestSimulatedAnnealing.J_valid,
                                    TestSimulatedAnnealing.h_valid,
                                    TestSimulatedAnnealing.offset_valid)
        drive_invalid_1 = [(1, observable), (1, observable)]
        schedule = Schedule(drive=drive_invalid_1)
        job = schedule.to_job()
        with pytest.raises(exceptions_types.QPUException):
            assert result == qpu_valid.submit_job(job)
        drive_invalid_2 = [(5, observable)]
        schedule = Schedule(drive=drive_invalid_2)
        job = schedule.to_job()
        with pytest.raises(exceptions_types.QPUException):
            assert result == qpu_valid.submit_job(job)

        # Solve the problem and check that the returned result is Result
        result = qpu_valid.submit_job(TestSimulatedAnnealing.job_valid)
        assert isinstance(result, Result)
Beispiel #2
0
    def test_reset(self):
        """test that reset gate works
        FIXME in 'analyze' mode, not testing intermediate_measurements
        FIXME not testing if resetting several qbits
        """
        # program with final state: qbit 0 : 0 with 100% prob,
        # but intermediate measure can be 0 or 1 with 50% prob
        prog = Program()
        reg = prog.qalloc(1)
        prog.apply(H, reg)
        prog.reset(reg)
        circ = prog.to_circ()

        ref_task = Task(circ, get_pylinalg_qpu())
        for res in ref_task.execute(nb_samples=5):

            self.assertEqual(res.state.int, 0)
            self.assertAlmostEqual(
                res.intermediate_measurements[0].probability, 0.5, delta=1e-10)
def test_reset():
    """
    Check the reset gate
    """
    # Define a program
    prog = Program()
    qbits = prog.qalloc(2)
    prog.apply(X, qbits[0])
    prog.reset(qbits)
    circ = prog.to_circ()

    # Submit circuit
    result = PyLinalg().submit(circ.to_job())

    # Check result
    assert len(result) == 1
    sample = result.raw_data[0]
    assert sample.state.int == 0

    # Check intermediate measurements
    assert len(sample.intermediate_measurements) == 1
    print(sample.intermediate_measurements)
    assert sample.intermediate_measurements[0].cbits == [True, False]
Beispiel #4
0
def qiskit_to_qlm(qiskit_circuit, sep_measures=False, **kwargs):
    """
    Converts a Qiskit circuit into a QLM circuit.

    Args:
        qiskit_circuit: The Qiskit circuit to convert
        sep_measures: Separates measures from the
            circuit:

             - if set to :code:`True`, measures won't be included in the resulting
               circuit, qubits to be measured will be put in a list, the resulting
               measureless circuit and this list will be returned in a tuple:
               (resulting_circuit, list_qubits)
             - if set to :code:`False`, measures will be converted normally
               (Default, set to False)

        kwargs: These are the options that you would use on a regular
                to_circ function, to generate a QLM circuit from a PyAQASM
                program these are added for more flexibility,
                for advanced users

    Returns:
        :code:`tuple` or :class:`~qat.core.Circuit`: If :code:`sep_measures` is set
        to:

         - :code:`True`: the result is a tuple composed of a
           :class:`~qat.core.Circuit` and a list of qubits that should be
           measured
         - :code:`False`: the result is a :class:`~qat.core.Circuit`
    """
    prog = Program()
    qbits_num = 0
    to_measure = []
    for reg in qiskit_circuit.qregs:
        qbits_num = qbits_num + reg.size
    qbits = prog.qalloc(qbits_num)

    cbits_num = 0
    for reg in qiskit_circuit.cregs:
        cbits_num = cbits_num + reg.size
    cbits = prog.calloc(cbits_num)
    variables = []
    for gate_op in qiskit_circuit.data:
        if gate_op[0].name in ("barrier", "opaque"):
            continue
        qbit_args = []
        cbit_args = []
        prms = []  # gate parameters
        # Get qbit arguments
        for qarg in gate_op[1]:
            qbit_args.append(
                _get_qindex(qiskit_circuit, qarg.register.name, qarg.index))

        # Get cbit arguments
        for carg in gate_op[2]:
            cbit_args.append(
                _get_cindex(qiskit_circuit, carg.register.name, carg.index))

        # Get parameters
        for param in gate_op[0].params:
            if isinstance(param, (Parameter, ParameterExpression)):
                prms.append(_qiskit_to_qlm_param(prog, variables, param))
            else:
                prms.append(float(param))
        # Apply measure #
        if gate_op[0].name == "measure":
            if sep_measures:
                to_measure.extend(qbit_args)
            else:
                prog.measure([qbits[i] for i in qbit_args],
                             [cbits[i] for i in cbit_args])
        elif gate_op[0].name == "reset":
            prog.reset([qbits[i] for i in qbit_args],
                       [cbits[i] for i in cbit_args])
        else:
            if gate_op[0].name == "ms":
                # In this case, the process function needs the number of qubits
                prms.append(len(qbit_args))
            # Apply gates #
            num_ctrl_qubits = None
            try:
                num_ctrl_qubits = gate_op[0].num_ctrl_qubits
            except AttributeError:
                pass
            gate = get_gate(gate_op[0].name, prms, num_ctrl_qubits)
            prog.apply(gate, *[qbits[i] for i in qbit_args][:gate.arity])
    if sep_measures:
        return prog.to_circ(**kwargs), list(set(to_measure))

    return prog.to_circ(**kwargs)