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

issue with the WBDD backend #4

Open
ondrik opened this issue Mar 24, 2024 · 5 comments
Open

issue with the WBDD backend #4

ondrik opened this issue Mar 24, 2024 · 5 comments

Comments

@ondrik
Copy link

ondrik commented Mar 24, 2024

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
Copy link
Collaborator

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
Copy link
Author

ondrik commented Mar 27, 2024

Hi Meghana,

thank you, we will try.

Best,
Ondra

@s-jobra
Copy link

s-jobra commented Mar 28, 2024

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
Copy link
Collaborator

cs14b052 commented Mar 28, 2024

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
Copy link

s-jobra commented Mar 29, 2024

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.

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

No branches or pull requests

3 participants