Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to run approximate simulation in Python? #224

Closed
rentristandelacruz opened this issue Apr 11, 2023 · 4 comments
Closed

[Question] How to run approximate simulation in Python? #224

rentristandelacruz opened this issue Apr 11, 2023 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@rentristandelacruz
Copy link

How do I run approximate simulations (fidelity-driven or memory-driven) in Python?

I am mainly using DDSim's ReadTheDocs.io documentation for information.
(https://ddsim.readthedocs.io/en/latest/)

The CircuitSimulator page (https://ddsim.readthedocs.io/en/latest/simulators/CircuitSimulator.html) mentions that the Circuit Simulator is the “default” simulator in DDSIM with the following features: 1) exact simulation, 2) weak simulation, and 3) approximate simulation with different strategies. It has two sections "Usage in Python" and "Usage as Standalone Executable" but I can't find any instructions on how to do approximate simulations in Python.

For the standalone executable, I run "./ddsim_simple --help" and I can see that the following options can be set in order to run approximate simulations:
--step_fidelity
--steps arg
--approx_when
--approx_state

I can't seem to find any instructions on how to set this options when using Python.

@hillmich hillmich added the question Further information is requested label Apr 11, 2023
@hillmich
Copy link
Contributor

Thank you for bringing this issue to our attention. Indeed there was a problem with generating the docs for the bindings. You can have a look at #225.

For future reference, the signature for the __init__ method of the CircuitSimulator is the following:

__init__(self: mqt.ddsim.pyddsim.CircuitSimulator, circ: object, approximation_step_fidelity: float = 1.0, approximation_steps: int = 1, approximation_strategy: str = 'fidelity', seed: int = -1) → None

Does this help?

@hillmich hillmich self-assigned this Apr 11, 2023
@rentristandelacruz
Copy link
Author

rentristandelacruz commented Apr 12, 2023

I see. Thank you! I am able to run approximate simulations now in Python.

I'm not yet familiar with DDSim's code. I saw the approximation options (approximation_step_fidelity, approximation_steps, approximation_strategy) in mqt/ddsim/qasmsimulator.py.

I thought I can pass these options to the execute function like this:

backendSimulator = ddsim.DDSIMProvider().get_backend("qasm_simulator")
myJob = execute(quantumCircuit, backendSimulator, shots=10000, approximation_step_fidelity=0.8, approximation_steps=2)

I thought it was similar to passing the mode and nthreads options to the execute function when using the hybrid_qasm_simulator backend.

backendSimulator = ddsim.DDSIMProvider().get_backend("hybrid_qasm_simulator")
myJob = execute(quantumCircuit, backendSimulator, shots=10000, mode="dd", nthreads=2)

@hillmich
Copy link
Contributor

The way you propose to use the approximation should work with the current version. The keyword paramaters are passed to the simulator.

The relevant snippet from qasmsimulator.py:

    def run_experiment(self, qobj_experiment: QasmQobjExperiment, **options) -> Dict:
        start_time = time.time()
        approximation_step_fidelity = options.get("approximation_step_fidelity", 1.0)
        approximation_steps = options.get("approximation_steps", 1)
        approximation_strategy = options.get("approximation_strategy", "fidelity")
        seed = options.get("seed", -1)

        sim = CircuitSimulator(
            qobj_experiment,
            approximation_step_fidelity=approximation_step_fidelity,
            approximation_steps=approximation_steps,
            approximation_strategy=approximation_strategy,
            seed=seed,
        )
        counts = sim.simulate(options.get("shots", 1024))
[...]

PR #225 adds some code to get rid of warnings and a simple test for the approximation. I'll add some documentation when I'll have time.

@rentristandelacruz
Copy link
Author

I initially did not notice that passing those options (approximation_step_fidelity, approximation_steps, approximation_strategy) to execute function actually works because I was focusing on warnings like:

/home/user./local/lib/python3.10/site-packages/qiskit/execute_function.py:378: UserWarning: Option approximation_step_fidelity is not used by this backend
  job = backend.run(experiments, **run_kwargs)

After checking again, I finally noticed that changing the options (specifically I was changing the value of approximation_step_fidelity) actually changed the result (shots/counts) of the job/experiment even if the warnings are there.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants