mqt-ddsim icon indicating copy to clipboard operation
mqt-ddsim copied to clipboard

✨ Memory Option

Open ahmadrv opened this issue 1 year ago • 7 comments

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.

ahmadrv avatar Dec 09 '23 21:12 ahmadrv

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).

burgholzer avatar Dec 09 '23 21:12 burgholzer

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.

ahmadrv avatar Dec 10 '23 07:12 ahmadrv

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.

burgholzer avatar Dec 10 '23 09:12 burgholzer

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

ahmadrv avatar Dec 10 '23 16:12 ahmadrv

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.

burgholzer avatar Dec 11 '23 19:12 burgholzer

Hi @burgholzer ,

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

ahmadrv avatar Jan 19 '24 11:01 ahmadrv

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.

burgholzer avatar Jan 24 '24 08:01 burgholzer