quil-lang / quilc

The optimizing Quil compiler.
Apache License 2.0
460 stars 72 forks source link

Add tests for equivalent Quil programs #274

Open appleby opened 5 years ago

appleby commented 5 years ago

As discussed in #257, it might be useful to add some tests that pass two programs with different Quil sources but the same "meaning" through the compiler or at least parse-quil and check that the matrices returned by make-matrix-from-quil or parsed-program-to-logical-matrix for the two programs satisfy matrix-equals-dwim.

Something like

(quil::matrix-equals-dwim
 (quil::parsed-program-to-logical-matrix
  (quil:compiler-hook (quil:parse-quil program-a) some-chip-spec))
 (quil::parsed-program-to-logical-matrix
  (quil::compiler-hook (quil:parse-quil program-b) some-chip-spec)))

or else

(quil::matrix-equals-dwim
 (quil::make-matrix-from-quil
  (coerce (quil::parsed-program-executable-code
           (quil:parse-quil program-a))
          'list))
 (quil::make-matrix-from-quil
  (coerce (quil::parsed-program-executable-code
           (quil:parse-quil program-b))
          'list)))

See the last couple of comments by @ecpeterson in this thread for more context, as well as a hint to look in rewriting-rules.lisp for examples of equivalent-but-different Quil snippets.

@ecpeterson also says in the linked thread that testing make-matrix-from-quil "... is certainly in the critical path, but it's also pretty low-risk."

notmgsk commented 5 years ago

I am almost certain that this is already tested, though maybe not directly. The express intent of the compiler is to produce output that is matrix-equals-dwim with the input. I would say (philosophically?) the entirety of the test suite amounts to testing that very condition.

appleby commented 5 years ago

I am almost certain that this is already tested, though maybe not directly. The express intent of the compiler is to produce output that is matrix-equals-dwim with the input. I would say (philosophically?) the entirety of the test suite amounts to testing that very condition.

There do exist a lot of tests where it's checked that the output of compilation is matrix-equal-dwim to the uncompiled input. What I'm suggesting is that given two programs whose sources differ but which conceptually "do the same thing" (scare quotes because I don't really understand what this means for a quantum program), check that their resulting logical matrices are matrix-equals-dwim. But maybe this not really a separate concept, and I am just too much of a quantum ignoramus to notice it :).

If people who know what they're talking about feel this is a low-value test, I'm happy to close this issue.

appleby commented 5 years ago

Thinking about this a bit more, I think I'm beginning to see the point that you and @ecpeterson have been trying to explain to me, albeit still dimly!

Based on my limited understanding, I guess any quantum program encodes some unitary matrix that acts on the initial state? So to say that two quantum programs "do the same thing" necessarily implies that they encode the same unitary, and therefore the fact that they have different Quil sources is maybe not that interesting of a distinction?

I still feel like there is something there that might be worth testing, but maybe I am just confused and trying to apply classical compiler/computing concepts in a quantum context where it doesn't make sense.