I'm trying to write unit tests for some small Q# operations. It would be ideal if I could access the wavefunction. Is there a way to get it?

I found Microsoft.Quantum.Diagnostics.DumpRegister, but it writes its output to the console or a file in a format intended for humans. I don't want to parse a non-trivial file format as part of writing a unit test.

  • $\begingroup$ Its access is made opaque. The DumpRegister seems unavoidable. $\endgroup$
    – cnada
    Dec 29 '18 at 4:07

For unit testing, you can use Assert* operations which allow you to verify that certain properties of the wavefunction match your expectations, for example, AssertProbInt operation or Microsoft.Quantum.Diagnostics namespace. The documentation mentions some of them here; you can also do "Filter by title" for library reference using "Assert" query and check which ones fit your specific goal best.

  • $\begingroup$ Those are much closer to what I need than I was able to find, but they all still need me to specify some additional operation (e.g. to use AssertAllZero I need to map the expected state to the 0 state). $\endgroup$ Dec 29 '18 at 5:28
  • $\begingroup$ Not necessarily; AssertProbInt checks that the amplitude of a certain basis state has the expected absolute value - if you have a state you don't need anything additional. If you can provide a more specific example of what you need I might be able to point you to the best Assert operation for that, I use a lot of them for my katas. $\endgroup$ Dec 29 '18 at 5:34
  • $\begingroup$ Well, for example, I want to assert that when you apply the QFT to a computational basis state then the output has amplitudes of equal magnitude with a consistent phase increase from amplitude to amplitude. Or, alternatively, I want to assert that my Grover oracle has negated the phase of the correct state. Or I want to verify that a diagonal operator had the right effect. $\endgroup$ Dec 30 '18 at 3:26
  • $\begingroup$ Oooooh, if I enable scripts on the docs site it has an actual search. That's somewhat more useful. $\endgroup$ Dec 30 '18 at 3:33
  • $\begingroup$ Another example of a complicated assertion is that I'd like to assert that the total weight of states with remainder other than 5 mod 13 is less than some tolerance. Currently I'd have to add new qubits and do simulated operations (very expensive!) in order to make an assertion like this. $\endgroup$ Dec 30 '18 at 3:38

Your Answer

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.