cda-tum / mqt-ddsim

MQT DDSIM - A quantum circuit simulator based on decision diagrams written in C++
https://mqt.readthedocs.io/projects/ddsim
MIT License
130 stars 27 forks source link

✨ Memory Option #326

Open ahmadrv opened 9 months ago

ahmadrv commented 9 months ago

What's the problem this feature will solve?

To whom it may concern, First of all, I apologize if there are some misleading with the way I present the problem.

I was tying to simulate Simon's algorithm with the ddsim backend when I ran into a problem. I didn't know if it was a problem with the missing feature or the implementation I did. This part is about calling the backend and getting the results:

backend = ddsim.DDSIMProvider().get_backend("qasm_simulator")
result = backend.run(transpiled_circuit, shots=num_shots, memory=True).result()

And here are the warning and error shown:

UserWarning: Option memory is not used by this backend
  result = backend.run(transpiled_circuit, shots=num_shots, memory=True).result()
Traceback (most recent call last):
  File "path/to/lib/python3.10/site-packages/qiskit/result/result.py", line 226, in get_memory
    memory = self.data(experiment)["memory"]
KeyError: 'memory'

Should the mentioned feature be implemented in the backend or am I doing something wrong? Thanks in advance for your tips.

Describe the solution you'd like

If I understand correctly and the mentioned feature is added, the implementation of Simon's algorithm will be as easy as other Qiskit backends.

burgholzer commented 9 months ago

Hey 👋🏻 Thanks for opening this issue!

Could you maybe elaborate a little more what kind of problem you are experiencing? Maybe in addition: what do you expect the memory option to do?

I am fairly sure that our backends support that feature if I have the right thing in mind. We just might not be exposing it to the Qiskit backends. That would be an easy fix (that shouldn't change any behavior though).

ahmadrv commented 9 months ago

Hey, As I mentioned I was trying to simulate Simon's algorithm with various backends. In the implementation of the algorithm based on IBM Quantum Learning, we should collect the results to prepare the appropriate information for finding the secret string $s$. As we pass True for the memory flag in:

from qiskit_aer import AerSimulator

def simon_measurements(problem: QuantumCircuit, k: int):
    """
    Quantum part of Simon's algorithm. Given a `QuantumCircuit` that
    implements f, get `k` measurements to be post-processed later.
    """
    n = problem.num_qubits // 2

    qc = QuantumCircuit(2 * n, n)
    qc.h(range(n))
    qc.compose(problem, inplace=True)
    qc.h(range(n))
    qc.measure(range(n), range(n))

    result = AerSimulator().run(qc, shots=k, memory=True).result()
    return result.get_memory()

the results of the circuit will be collected. In my experience with ddsim.DDSIMProvider() with this implementation:

from mqt import ddsim

def simon_measurements(problem: QuantumCircuit, k: int):
    """
    Quantum part of Simon's algorithm. Given a `QuantumCircuit` that
    implements f, get `k` measurements to be post-processed later.
    """
    n = problem.num_qubits // 2

    qc = QuantumCircuit(2 * n, n)
    qc.h(range(n))
    qc.compose(problem, inplace=True)
    qc.h(range(n))
    qc.measure(range(n), range(n))

    result = ddsim.DDSIMProvider().get_backend("qasm_simulator").run(qc, shots=k, memory=True).result()
    return result.get_memory()

I couldn't collect the results in the same way and the mentioned error was raised.

burgholzer commented 9 months ago

Thanks for the explanation! In fact, we do not currently implement the feature that you can get the sequence of measurement results. If I am not mistaken, that shouldn't be too hard to realize. I might find some time next week to add that feature!

If you are only interested in the measurement results and not their particular sequence, you can just use the result.get_counts() method (without the memory flag being set to avoid the warning). That gives you a dictionary of measurement results and corresponding counts. Maybe a suitable workaround for now.

ahmadrv commented 9 months ago

It would be nice if the feature is added. Thanks for the mentioned workaround and your time. 🙏🏻

burgholzer commented 9 months ago

You are welcome.

I'll keep this issue open for now to track the feature request. I'll close once the PR implementing the feature is merged.

ahmadrv commented 8 months ago

Hi @burgholzer ,

I wanted to ask if there was any news about adding the feature?

burgholzer commented 7 months ago

Hi @burgholzer ,

I wanted to ask if there was any news about adding the feature?

Hey 👋🏼

This is definitely still on our radar. At the moment, there are just a couple more urgent things that need to be dealt with.