# Testing superposition states in Q#

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?

• I'm not familiar with Q#, but from the looks of it you could change the 'PauliZ' to 'PauliX' to check if the state is an eigenstate of the X operator - that's because the Hadamard transforms the eigenstates of the Pauli Z to the Pauli X operator (and vice-versa, btw) – JSdJ Aug 7 '20 at 14:57

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!

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 |0\rangle \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 $$|0\rangle$$ and ensure it always returns $$|0\rangle$$.

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: $$|0\rangle \mapsto \frac{1}{\sqrt{2}}(|0\rangle + e^{i\theta} |1\rangle)$$). 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.

• Wow, I didn't realise that it was that hard to test a chip. Its true that we don't have DumpMachine() in there :) – BrockenDuck Aug 7 '20 at 17:29
• You opened a new world to me! I accepted the other answer because it's more specific about Q# with some code about implementing the first point that you mention (I was expecting something like that in Q#), but I was not aware of all this problems when considering the real hardware. Thank you very much! – Daniele Armanasco Aug 11 '20 at 16:39