2
$\begingroup$

I want to examine the graph of 2 sets consisting of 1000 numbers created with quantum random number generator which were created by entangled qubits and see if there is a pattern in the randomly generated number series.

How can I do this with Q#?

So by doing that I think,I can entangle two qubits in Sample Quantum Random Number Generator

operation SampleQuantumRandomNumberGenerator() : Result {
// Allocate two qubits
using ((q1, q2) = (Qubit(), Qubit()))  {
    // Prepare Bell state (|00⟩ + |11⟩) / sqrt(2) on them
    H(q1);
    CNOT(q1, q2);
    // The measurement results are going to be correlated: you get 0,0 in 50% of the cases and 1,1 in 50%
    return (MResetZ(q1) == Zero ? 0 | 1, 
            MResetZ(q2) == Zero ? 0 | 1);
}

But how can I use qubits ( qs(0) for one set qs(1) for one set ) in here for getting 2 sets consisting of 1000 numbers between 0 and 100 ?

operation SampleRandomNumberInRange(max : Int) : Int {
    mutable bits = new Result[0];
    for (idxBit in 1..BitSizeI(max)) {
        set bits += [SampleQuantumRandomNumberGenerator()];
    }
    let sample = ResultArrayAsInt(bits);
    return sample > max
           ? SampleRandomNumberInRange(max)
           | sample;
}

@EntryPoint()
operation SampleRandomNumber() : Int {
    let max = 100;
    Message($"Sampling a random number between 0 and {max}: ");
    return SampleRandomNumberInRange(max);
}
New contributor
theRomanMercury is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
  • $\begingroup$ If it were me, I'd pull the data into Matlab. That kind of analysis would be trivial there. I'm not sure exactly what sort of correlation you're looking for, but for anything like this I always start by plotting the data in both the time and frequency domains to get a quick visual sense of the data set and to catch any obvious anomalies. $\endgroup$ – Jonathan Trousdale May 22 at 13:14
  • $\begingroup$ quantum random number generator is needed for it $\endgroup$ – theRomanMercury May 22 at 13:14
  • $\begingroup$ Sorry, I think I misunderstood. You need to write a QRNG in Q#? $\endgroup$ – Jonathan Trousdale May 22 at 13:15
  • $\begingroup$ Yeah there is a QRNG at Q# but I want to make it with 2 qubits which are entengled and each of them generates random numbers $\endgroup$ – theRomanMercury May 22 at 13:17
  • 1
    $\begingroup$ now I get it, the formulas are not understood when looking on the phone :) $\endgroup$ – theRomanMercury May 23 at 8:50
2
$\begingroup$

You can base your code on this Q# sample, adjacent to the one you've been looking at.


The simplest thing is generating random bits 0 or 1 that are perfectly correlated; you can do that using Bell state $|\Phi^+\rangle$:

operation GenerateCorrelatedRandomNumbers () : (Int, Int) {
    // Allocate two qubits
    using ((q1, q2) = (Qubit(), Qubit()))  {
        // Prepare Bell state (|00⟩ + |11⟩) / sqrt(2) on them
        H(q1);
        CNOT(q1, q2);
        // The measurement results are going to be correlated: you get 0,0 in 50% of the cases and 1,1 in 50%
        return (MResetZ(q1) == Zero ? 0 | 1, 
                MResetZ(q2) == Zero ? 0 | 1);
    }
}

@EntryPoint()
operation SampleCorrelatedRandomNumbers () : Unit {
    for (i in 1 .. 10) {
        Message($"{GenerateCorrelatedRandomNumbers()}");
    }
}

This will give you

(0, 0)
(1, 1)
(1, 1)
...
(0, 0)

If you want the bits to be perfectly anti-correlated, you can use the state $|\Psi^+\rangle$:

    using ((q1, q2) = (Qubit(), Qubit()))  {
        // Prepare Bell state (|10⟩ + |01⟩) / sqrt(2) on them
        H(q1);
        CNOT(q1, q2);
        X(q1);
        // The measurement results are going to be correlated: you get 0,1 in 50% of the cases and 1,0 in 50%
        return (MResetZ(q1) == Zero ? 0 | 1, 
                MResetZ(q2) == Zero ? 0 | 1);
    }

  • If you want your bits to still be correlated but yield outcomes with different probabilities than 50%/50%, you can use a rotation gate Ry instead of H to prepare a state $\alpha |00\rangle + \beta |11\rangle$ - that will give you (0,0) with probability $\alpha^2$ and (1,1) with probability $\beta^2$ (you don't need to use complex coefficients if you only care about simple measurement probabilities).
  • If you want your bits to be correlated strongly but not perfectly, you can prepare a superposition of all basis states with different amplitudes - for example, something like $\frac{1}{\sqrt{20}}(3|00\rangle + |01\rangle + |10\rangle + 3|11\rangle)$ will give you equal bits in 90% of the cases and distinct bits in 10% of the cases.
  • You can learn more about Q# programming and preparing quantum states using Q# in the Quantum Katas - the first set of tutorials and exercises focuses on the basic constructs like allocating qubits and applying gates and preparing states on them.

To address the updated question:

You can allocate two registers of 7 qubits each to generate pairs of 7-bit integers (up to 128), entangle qubits of two registers between themselves to force correlations that you want, similar to how I've shown in the preparation of Bell state, and filter out the generated numbers that end up being greater than 100, as shown in the code in your question. To generate a 1000 pairs, you can call the Q# code from classical driver multiple times and aggregate the results; this might be better than doing it in Q#, since then you'll be able to use Python libraries to analyze and visualize the correlations nicely.

I'm not providing the code here, as I believe you have all the pieces to build it yourself, and such an exercise will be very beneficial.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your answer, I updated my question with your help. $\endgroup$ – theRomanMercury May 23 at 23:04
  • $\begingroup$ @theRomanMercury I updated the answer respectively. $\endgroup$ – Mariia Mykhailova May 24 at 0:26

Your Answer

theRomanMercury is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.