Skip to content

Commit

Permalink
Allow mx and my gates in OpenQasm2 translation (#2494)
Browse files Browse the repository at this point in the history
* Allow mx and my gates in OpenQasm2 translation

Signed-off-by: Anna Gringauze <[email protected]>

* Address CR comments

Signed-off-by: Anna Gringauze <[email protected]>

---------

Signed-off-by: Anna Gringauze <[email protected]>
  • Loading branch information
annagrin authored Jan 8, 2025
1 parent 2137a4a commit da44eda
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 5 deletions.
1 change: 1 addition & 0 deletions python/runtime/cudaq/platform/py_alt_launch_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ std::string getASM(const std::string &name, MlirModule module,
"CHToCX", "CCZToCX", "CRzToCX", "CRyToCX", "CRxToCX",
"CR1ToCX", "CCZToCX", "RxAdjToRx", "RyAdjToRy",
"RzAdjToRz"}}));
pm.addPass(cudaq::opt::createQuakeToCCPrep());
pm.addNestedPass<func::FuncOp>(createCanonicalizerPass());
pm.addNestedPass<func::FuncOp>(cudaq::opt::createExpandControlVeqs());
pm.addNestedPass<func::FuncOp>(cudaq::opt::createCombineQuantumAllocations());
Expand Down
4 changes: 2 additions & 2 deletions python/tests/backends/test_braket.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def single_qubit_gates():
rz(np.pi, q)
s(q)
t(q)
# mx(q) ## Unsupported
# my(q) ## Unsupported
mx(q)
my(q)
mz(q)

# Test here is that this runs
Expand Down
174 changes: 173 additions & 1 deletion python/tests/mlir/test_output_translate_openqasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import cudaq


def test_synth_and_openqasm():
def test_openqasm2_synth():

@cudaq.kernel
def ghz(numQubits: int):
Expand All @@ -35,3 +35,175 @@ def ghz(numQubits: int):
# CHECK: cx var0[1], var0[2];
# CHECK: cx var0[2], var0[3];
# CHECK: cx var0[3], var0[4];

def test_openqasm2_gates():

@cudaq.kernel
def gates():
q = cudaq.qubit()
ctrl0 = cudaq.qubit()
ctrl1 = cudaq.qubit()
h(q)
x(q)
y(q)
z(q)
s(q)
t(q)
h.ctrl(ctrl0, q)
z.ctrl(ctrl0, ctrl1, q)
rx.ctrl(np.pi, ctrl0, q)
ry.ctrl(np.pi, ctrl0, q)
rz.ctrl(np.pi, ctrl0, q)
r1.ctrl(np.pi, ctrl0, q)
r1(np.pi, q)
rx(np.pi, q)
rx.adj(np.pi, q)
ry(np.pi, q)
ry.adj(np.pi, q)
rz(np.pi, q)
rz.adj(np.pi, q)
mx(q)
my(q)
mz(q)

print(cudaq.translate(gates, format="openqasm2"))

# CHECK: // Code generated by NVIDIA's nvq++ compiler
# CHECK: OPENQASM 2.0;

# CHECK: include "qelib1.inc";

# CHECK: qreg var0[3];
# CHECK: h var0[0];
# CHECK: x var0[0];
# CHECK: y var0[0];
# CHECK: z var0[0];
# CHECK: rz(1.570796e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rz(7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rz(1.570796e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: h var0[0];
# CHECK: rz(7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(-7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: h var0[0];
# CHECK: rz(-1.570796e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(-7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[2], var0[0];
# CHECK: rz(7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(-7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[2], var0[0];
# CHECK: rz(7.853982e-01) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[2], var0[1];
# CHECK: rz(-7.853982e-01) var0[1];
# CHECK: rx(1.570796e+00) var0[1];
# CHECK: rz(0.000000e+00) var0[1];
# CHECK: rx(-1.570796e+00) var0[1];
# CHECK: rz(0.000000e+00) var0[1];
# CHECK: cx var0[2], var0[1];
# CHECK: rz(7.853982e-01) var0[1];
# CHECK: rx(1.570796e+00) var0[1];
# CHECK: rz(0.000000e+00) var0[1];
# CHECK: rx(-1.570796e+00) var0[1];
# CHECK: rz(0.000000e+00) var0[1];
# CHECK: rz(7.853982e-01) var0[2];
# CHECK: rx(1.570796e+00) var0[2];
# CHECK: rz(0.000000e+00) var0[2];
# CHECK: rx(-1.570796e+00) var0[2];
# CHECK: rz(0.000000e+00) var0[2];
# CHECK: rz(1.570796e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: ry(-1.570796e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: ry(1.570796e+00) var0[0];
# CHECK: rz(-1.570796e+00) var0[0];
# CHECK: ry(1.570796e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: ry(-1.570796e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(1.570796e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(-1.570796e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(1.570796e+00) var0[1];
# CHECK: rx(1.570796e+00) var0[1];
# CHECK: rz(0.000000e+00) var0[1];
# CHECK: rx(-1.570796e+00) var0[1];
# CHECK: rz(0.000000e+00) var0[1];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(-1.570796e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: cx var0[1], var0[0];
# CHECK: rz(1.570796e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rz(3.141593e+00) var0[0];
# CHECK: rx(1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(-1.570796e+00) var0[0];
# CHECK: rz(0.000000e+00) var0[0];
# CHECK: rx(3.141593e+00) var0[0];
# CHECK: rx(-3.141593e+00) var0[0];
# CHECK: ry(3.141593e+00) var0[0];
# CHECK: ry(-3.141593e+00) var0[0];
# CHECK: rz(3.141593e+00) var0[0];
# CHECK: rz(-3.141593e+00) var0[0];
# CHECK: h var0[0];
# CHECK: creg var4[1];
# CHECK: measure var0[0] -> var4[0];
# CHECK: sdg var0[0];
# CHECK: h var0[0];
# CHECK: creg var5[1];
# CHECK: measure var0[0] -> var5[0];
# CHECK: creg var6[1];
# CHECK: measure var0[0] -> var6[0];
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ config:
# Tell NVQ++ to generate glue code to set the target backend name
gen-target-backend: true
# Define the lowering pipeline
platform-lowering-config: "func.func(const-prop-complex,canonicalize,cse,lift-array-alloc),globalize-array-values,func.func(state-prep),unitary-synthesis,canonicalize,apply-op-specialization,aggressive-early-inlining,unrolling-pipeline,func.func(lower-to-cfg,canonicalize,multicontrol-decomposition),decomposition{enable-patterns=SToR1,TToR1,R1ToU3,U3ToRotations,CHToCX,CCZToCX,CRzToCX,CRyToCX,CRxToCX,CR1ToCX,RxAdjToRx,RyAdjToRy,RzAdjToRz},func.func(expand-control-veqs,combine-quantum-alloc,canonicalize,combine-measurements)"
platform-lowering-config: "func.func(const-prop-complex,canonicalize,cse,lift-array-alloc),globalize-array-values,func.func(state-prep),unitary-synthesis,canonicalize,apply-op-specialization,aggressive-early-inlining,unrolling-pipeline,func.func(lower-to-cfg,canonicalize,multicontrol-decomposition),decomposition{enable-patterns=SToR1,TToR1,R1ToU3,U3ToRotations,CHToCX,CCZToCX,CRzToCX,CRyToCX,CRxToCX,CR1ToCX,RxAdjToRx,RyAdjToRy,RzAdjToRz},quake-to-cc-prep,func.func(expand-control-veqs,combine-quantum-alloc,canonicalize,combine-measurements)"
# Tell the rest-qpu that we are generating OpenQASM 2.0.
codegen-emission: qasm2
# Library mode is only for simulators, physical backends must turn this off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ config:
# Tell NVQ++ to generate glue code to set the target backend name
gen-target-backend: true
# Define the lowering pipeline
platform-lowering-config: "func.func(const-prop-complex,canonicalize,cse,lift-array-alloc),globalize-array-values,func.func(state-prep),unitary-synthesis,canonicalize,apply-op-specialization,aggressive-early-inlining,unrolling-pipeline,func.func(lower-to-cfg),canonicalize,func.func(multicontrol-decomposition),decomposition{enable-patterns=SToR1,TToR1,CCZToCX,CRyToCX,CRxToCX,R1AdjToR1,RxAdjToRx,RyAdjToRy,RzAdjToRz},func.func(memtoreg{quantum=0}),symbol-dce"
platform-lowering-config: "func.func(const-prop-complex,canonicalize,cse,lift-array-alloc),globalize-array-values,func.func(state-prep),unitary-synthesis,canonicalize,apply-op-specialization,aggressive-early-inlining,unrolling-pipeline,func.func(lower-to-cfg),canonicalize,func.func(multicontrol-decomposition),decomposition{enable-patterns=SToR1,TToR1,CCZToCX,CRyToCX,CRxToCX,R1AdjToR1,RxAdjToRx,RyAdjToRy,RzAdjToRz},quake-to-cc-prep,func.func(memtoreg{quantum=0}),symbol-dce"
# Tell the rest-qpu that we are generating OpenQASM 2.0.
codegen-emission: qasm2
# Library mode is only for simulators, physical backends must turn this off
Expand Down

0 comments on commit da44eda

Please sign in to comment.