trishullab / Quasimodo

MIT License
6 stars 6 forks source link

issue with the WBDD backend #4

Open ondrik opened 6 months ago

ondrik commented 6 months ago

When using Quasimodo for quantum circuit simulation with @s-jobra, there seems to be some issue with doing Hadamard using the WBDD backend. In particular, the following program

#include <iostream>
#include <unordered_map>
#include <string>
#include "quantum_circuit.h"

#define N_SHOTS 1024

int main()
{
    BDDQuantumCircuit bddCirc;
    WeightedBDDQuantumCircuit wbddCirc;

    bddCirc.setNumQubits(1);
    bddCirc.ApplyHadamardGate(0);

    wbddCirc.setNumQubits(1);
    wbddCirc.ApplyHadamardGate(0);

    std::unordered_map<std::string, int> bddMeasureResults;
    std::unordered_map<std::string, int> wbddMeasureResults;
    for(int i = 0; i < N_SHOTS; i++) {
        bddMeasureResults[bddCirc.Measure()]++;
        wbddMeasureResults[wbddCirc.Measure()]++;
    }

    std::cout << "Hadamard sampled results - BDD:" << std::endl;
    for(const auto& pair : bddMeasureResults) {
        std::cout << "    '" << pair.first << "' : " << pair.second << std::endl;
    }
    std::cout << "Hadamard sampled results - WBDD:" << std::endl;
    for(const auto& pair : wbddMeasureResults) {
        std::cout << "    '" << pair.first << "' : " << pair.second << std::endl;
    }
}

outputs

Hadamard sampled results - BDD:
    '1' : 504
    '0' : 520
Hadamard sampled results - WBDD:
    '' : 1024

Do you know what could be the issue? Best, Ondra

cs14b052 commented 6 months ago

It may be due to stale code. Please check now and let me know. Also, the WBDD package is not being maintained now. We now use https://github.com/cda-tum/dd_package package. You can use the MQTDDQuantumCircuit class in quantum_circuit.h to use the Weighted BDD backend.

ondrik commented 6 months ago

Hi Meghana,

thank you, we will try.

Best, Ondra

s-jobra commented 6 months ago

Hi,

now the previously sent code outputs

Hadamard sampled results - BDD:
    '1' : 504
    '0' : 520
Hadamard sampled results - WBDD:
    '0' : 1024

The MQTDDQuantumCircuit class behaves as expected. However, right now I was only able to build and use Quasimodo after a few changes.

Also, it seems there is now some issue with Hadamards and the BDD backend instead. For example, when I run this

#include <iostream>
#include <unordered_map>
#include <string>
#include "quantum_circuit.h"

#define N_SHOTS 1024

int main()
{
    BDDQuantumCircuit bddCirc;
    MQTDDCircuit wbddCirc;

    bddCirc.setNumQubits(2);
    bddCirc.ApplyHadamardGate(0);
    bddCirc.ApplyHadamardGate(1);
    bddCirc.ApplyHadamardGate(0);
    bddCirc.ApplyHadamardGate(1);

    wbddCirc.setNumQubits(2);
    wbddCirc.ApplyHadamardGate(0);
    wbddCirc.ApplyHadamardGate(1);
    wbddCirc.ApplyHadamardGate(0);
    wbddCirc.ApplyHadamardGate(1);

    std::unordered_map<std::string, int> bddMeasureResults;
    std::unordered_map<std::string, int> wbddMeasureResults;
    for(int i = 0; i < N_SHOTS; i++) {
        bddMeasureResults[bddCirc.Measure()]++;
        wbddMeasureResults[wbddCirc.Measure()]++;
    }

    std::cout << "Two Hadamards sampled results - BDD:" << std::endl;
    for(const auto& pair : bddMeasureResults) {
        std::cout << "    '" << pair.first << "' : " << pair.second << std::endl;
    }
    std::cout << "Two Hadamards sampled results - WBDD:" << std::endl;
    for(const auto& pair : wbddMeasureResults) {
        std::cout << "    '" << pair.first << "' : " << pair.second << std::endl;
    }
}

it outputs

Two Hadamards sampled results - BDD:
    '00' : 545
    '01' : 479
Two Hadamards sampled results - WBDD:
    '00' : 1024

When I run this program on the previous version, the BDD backend behaves as expected.

Best regards, Sara

cs14b052 commented 6 months ago

Thanks for the PR. Looks like I missed it when merging branches.

I tried using the provided Python interface and ran your code. It behaves as expected. Let me take a look at why directly importing C++ code is causing an issue. Can you try your code with the Python interface? This is the code I used:

` import sys import quasimodo import time import random

NSHOTS = 1024

numQubits = int(sys.argv[1])

random.seed(int(sys.argv[3]))

qc = quasimodo.QuantumCircuit(sys.argv[2], numQubits, int(sys.argv[3]))

qc.h(0) qc.h(1) qc.h(0) qc.h(1)

samples = {}

for i in range(NSHOTS): sampled_string = qc.measure() if sampled_string in samples: samples[sampled_string] += 1 else: samples[sampled_string] = 1

print (samples)

`

The output:

{'00': 1024}

for both BDD and MQTDD backends.

Call the above program as - python3 program.py 2 . Eg: python3 program.py 2 BDD 0

s-jobra commented 6 months ago

Your python program also works for me. Could the problem be caused by not using the seed in the C++ code? I wasn't sure what it was for.