epiqc / ScaffCC

Compilation, analysis and optimization framework for the Scaffold quantum programming language.
BSD 2-Clause "Simplified" License
187 stars 53 forks source link

CTQG compilation example: controlled not #44

Closed kgi-github closed 4 years ago

kgi-github commented 4 years ago

Hi,

I am new to ScaffCC so thank you for making the code available and apologies if I'm making obvious errors.

I am interested particularly in the CTQG functionality. I have written a simple CONTROLLED NOT module:

/* begin ctqg */
scaff_module my_c_not(qbit q[2])
{
        if (q[0] == 1) {
                X(q[1]);
        }
}
/* end ctqg */

int main() {
        qbit r[2];
        PrepZ(r[0], 0);
        PrepZ(r[1], 0);
        H(r[0]);
        my_c_not(r);
}

however the qasm produced by the scaffold compiler is

module my_c_not(qbit *q){
        X ( q[1] );
 }

module main(){
        qbit r[2];
        H ( r[0] );
        my_c_not ( r );
 }

which is simply a NOT instead of a CONTROLLED NOT. Note the first qubit has had a HADAMARD gate applied. Do I need to do something special to compile CTQG code? Is it correct to write

if (q[0] == 1)

?

Also is there a way to simulate my program? I'm running on a Linux box.

Thanks for your help, Kevin.

AndrewLitteken commented 4 years ago

A CNOT gate is a built in primitive into Scaffold, so if it is only that aspect of controlled gates, I would recommend just using that. The syntax is CNOT(control, target)

If you're asking about implementing controlled gates in Scaffold in general, you cannot use if statements on a quantum bit at present. The generated code will be what you experienced, essentially the body being generated. This is due to the fact that when you're designing a circuit in Scaffold, having a structure like

if (q[0] == 1) {
  X(q[1]);
  Y(q[1]);
}

has a lot of edge cases and design decisions that are hard to grapple with, and haven't been dealt with yet. The best solution is to create modules that contain decompositions of the gates you need. So, I'm sorry for any inconvenience on that front.

The CTQG code has since been removed from Scaffold, however there are builtin functions that deal with addition, and swapping. You can see what these all are if you look in this test.

To simulate the generated code, I'd recommend generating OpenQASM code (-b in the compilation script) and passing to that to a simple Python script that uses Qiskit to read in the OpenQASM and run it in a simulator. You can look in the nisq_benchmarks for example code doing that.

kgi-github commented 4 years ago

Hi,

Thanks for your helpful reply. The cnot was just an example, I'm hoping to compile more substantial classical logic into quantum gates. Do you know of anything out there that does this, or at least compiles into reversible gates?

kgi-github commented 4 years ago

Hi again, May I ask why the CTQG code was removed? If it's usable, is it possible to get a copy of it? cheers, Kevin.

AndrewLitteken commented 4 years ago

RQKC, another project in EPiQC should have CTGQ capabilities. It was removed from Scaffold due to permissions for open sourcing the original code, RKQC is a different implementation of this.

kgi-github commented 4 years ago

Thanks Andrew. I am trying to build RKQC, but it's not straightforward as it is 4 years old and some dependencies are missing. However from the documentation it doesn't look like it will cope with my c-not program, in particular none of the documents or examples show an if statement on a quantum bit. Do you know if it supports this? Also, what became of the CTGQ code (even if it isn't open source)?

AndrewLitteken commented 4 years ago

I'm not really familiar with RKQC, I am really only familiar with the most recent release of Scaffold. The CTGQ code was removed in a prior commit. If you want to try, you can go back and clone the repository at an older point to try to find it. I don't think that will do what you want with a conditional on a qubit either. I'm not really aware of any programs that could directly translate a a structure like that to QASM.

kgi-github commented 4 years ago

Thanks Andrew!