TransWikia.com

Testing superposition states in Q#

Quantum Computing Asked by Daniele Armanasco on September 16, 2020

I am learning Q# so, as I often do, I am writing some unit tests to play with quantum gates.
I was able to write these tests for X gate:

    @Test("QuantumSimulator")
    operation XGateAppliedToZeroShouldTransformIntoOne() : Unit {
        
        using (q = Qubit()) {
            X(q);
            Assert([PauliZ], [q], One, "X gate transform |0> state in |1> state.");

            X(q);
            Assert([PauliZ], [q], Zero, "X gate transform |1> state in |0> state.");
        }
        
        Message("Test passed.");
    }

I am wondering how to write similar tests for the H gate: from what I know it will put the system in a superposition state that will give me 0 or 1 with a probability of 50%. How can I test an expected probability? Am I completely in the wrong direction here?

2 Answers

As @JSdJ indicated in their comment, one approach is to perform the assertion in the ?-basis instead of the ?-basis:

    open Microsoft.Quantum.Diagnostics;

    @Test("QuantumSimulator")
    operation CheckThatHPreparesPlus() : Unit {        
        using (q = Qubit()) {
            within { H(q); }
            apply {
                AssertMeasurement([PauliZ], [q], Zero, "H operation did not prepare |+⟩, given input in |0⟩.");
            }
        }
    }

Another approach would be to use the AssertMeasurementProbability operation, which asserts that the probability of a given hypothetical measurement is equal to an expected probability:

    open Microsoft.Quantum.Diagnostics;

    @Test("QuantumSimulator")
    operation CheckThatHPreparesPlus() : Unit {        
        using (q = Qubit()) {
            within { H(q); }
            apply {
                AssertMeasurementProbability(
                    [PauliZ], [q], Zero, 0.5,
                    "State prepared by H operation does not admit 50/50 measurement in ? basis.", 1e-8
                );
            }
        }
    }

This second approach checks a weaker condition than the asserting in the ?-basis, since measuring a qubit in either the |+⟩ or |−⟩ state will result in 50/50 probabilities when measured in the ?-basis, but only the |+⟩ has a 100% probability of returning a Zero when measured in the ?-basis.

Either way, though, one thing to keep in mind is that these assertions use that they're run on a simulator to do their job. By design, these assertions can be safely stripped when run on hardware, such that the no-cloning theorem applies. As Chris Kang notes in his answer, to test that hardware does what you expect can be involve some fairly different techniques compared to using a simulator to check that your program does what you expect. There's a lot of neat research out there (that's my main area of research, for example) on how to do that — you might find quantum tomography to be an interesting place to start!

Correct answer by Chris Granade on September 16, 2020

Welcome Daniele! It's a fantastic question - some forms of physical hardware can measure on different axis', so you could verify the qubit state by measuring with the $| + rangle $ and $ |-rangle$ basis states (and, if you got $|+rangle$ with high probability, you could assume $H |0rangle mapsto |+rangle$).

In Q#, I don't believe this has yet been implemented. Some alternative approaches include:

  • Measuring the qubit directly over many trials, expecting about a 50/50 probability of getting 0 or 1.
  • If you have verified the $X$ gate, you could perform $ HZH $ and ensure it behaves like $X$.
  • If you have verified the $X$ gate, you could perform $HXH$ on $|0rangle$ and ensure it always returns $|0rangle$.

For Q# programming, you can probably trust the programmers/quantum runtime! But you open up a great secondary question- how would we verify a real quantum chip, especially because we can't just ask the universe for amplitudes :)

More broadly, verifying the validity of quantum operations is a critical but challenging task. One way, of course, is getting the right answer: if we ran a chemistry simulation and correctly predicted the molecule's bond length, we are good to go.

For questions without easily verifiable answers, the solutions become more challenging! For example, the approach listed where we simply randomly sample omits potential phase changes (think: $ |0rangle mapsto frac{1}{sqrt{2}}(|0rangle + e^{itheta} |1rangle)$). What do we do in that case? One approach tries to reconstruct the state via a quantum neural network, but overall I think this is an active field of research with some awesome open-ended questions to be asked/answered.

Answered by C. Kang on September 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP