Example #1
0
def sim(request):
    if request.param == "cpp_simulator":
        from projectq.backends._sim._cppsim import Simulator as CppSim
        sim = Simulator(gate_fusion=True)
        sim._simulator = CppSim(1)
        return sim
    if request.param == "py_simulator":
        from projectq.backends._sim._pysim import Simulator as PySim
        sim = Simulator()
        sim._simulator = PySim(1)
        return sim
Example #2
0
def sim(request):
    if request.param == "cpp_simulator":
        from projectq.backends._sim._cppsim import Simulator as CppSim
        sim = Simulator(gate_fusion=True)
        sim._simulator = CppSim(1)
        # If an ImportError occurs, the C++ simulator was either not installed
        # or compiled for a different Python version.
        return sim
    if request.param == "py_simulator":
        from projectq.backends._sim._pysim import Simulator as PySim
        sim = Simulator()
        sim._simulator = PySim(1)
        return sim
Example #3
0
def test_simulator_constant_math_emulation():
    if "cpp_simulator" not in get_available_simulators():
        pytest.skip("No C++ simulator")
        return

    results = [[[1, 1, 0, 0, 0]], [[0, 1, 0, 0, 0]], [[0, 1, 1, 1, 0]]]

    import projectq.backends._sim._simulator as _sim
    from projectq.backends._sim._pysim import Simulator as PySim
    from projectq.backends._sim._cppsim import Simulator as CppSim
    from projectq.libs.math import (AddConstant, AddConstantModN,
                                    MultiplyByConstantModN)

    def gate_filter(eng, cmd):
        g = cmd.gate
        if isinstance(g, BasicMathGate):
            return False
        return eng.next_engine.is_available(cmd)

    def run_simulation(sim):
        eng = MainEngine(sim, [])
        quint = eng.allocate_qureg(5)
        AddConstant(3) | quint
        All(Measure) | quint
        eng.flush()
        results[0].append([int(qb) for qb in quint])

        AddConstantModN(4, 5) | quint
        All(Measure) | quint
        eng.flush()
        results[1].append([int(qb) for qb in quint])

        MultiplyByConstantModN(15, 16) | quint
        All(Measure) | quint
        eng.flush()
        results[2].append([int(qb) for qb in quint])

    cppsim = Simulator(gate_fusion=False)
    cppsim._simulator = CppSim(1)
    run_simulation(cppsim)

    _sim.FALLBACK_TO_PYSIM = True
    pysim = Simulator()
    pysim._simulator = PySim(1)
    # run_simulation(pysim)

    for result in results:
        ref = result[0]
        for res in result[1:]:
            assert ref == res
Example #4
0
def test_simulator_flush():
    sim = Simulator()
    sim._simulator = MockSimulatorBackend()

    eng = MainEngine(sim)
    eng.flush()

    assert sim._simulator.run_cnt == 1