Skip to content

Commit

Permalink
dialects: (gate) add quaternion gate
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarice committed Oct 29, 2024
1 parent b2945da commit 73eaa4c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
44 changes: 42 additions & 2 deletions inconspiquous/dialects/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import ClassVar

from xdsl.dialects.builtin import FloatAttr, Float64Type
from xdsl.ir import Dialect, ParametrizedAttribute, TypeAttribute
from xdsl.ir import Dialect, Operation, ParametrizedAttribute, SSAValue, TypeAttribute
from xdsl.irdl import (
AttrConstraint,
IRDLOperation,
Expand All @@ -13,10 +13,11 @@
base,
irdl_attr_definition,
irdl_op_definition,
operand_def,
prop_def,
result_def,
)
from xdsl.parser import AttrParser, IndexType, IntegerAttr
from xdsl.parser import AnyFloatConstr, AttrParser, IndexType, IntegerAttr, IntegerType
from xdsl.printer import Printer
from xdsl.traits import ConstantLike, Pure

Expand Down Expand Up @@ -209,10 +210,49 @@ def __init__(self, gate: GateAttr):
)


@irdl_op_definition
class QuaternionGateOp(IRDLOperation):
"""
A gate described by a quaternion.
The action of the gate on the Bloch sphere is given by the rotation generated
by conjugating by the quaternion.
"""

_T: ClassVar = VarConstraint("T", base(IntegerType) | AnyFloatConstr)

name = "gate.quaternion"

real = operand_def(_T)
i = operand_def(_T)
j = operand_def(_T)
k = operand_def(_T)

out = result_def(GateType(1))

assembly_format = (
"`<` type($real) `>` $real `+` $i `i` `+` $j `j` `+` $k `k` attr-dict"
)

def __init__(
self,
real: Operation | SSAValue,
i: Operation | SSAValue,
j: Operation | SSAValue,
k: Operation | SSAValue,
):
real = SSAValue.get(real)
super().__init__(
operands=(real, i, j, k),
result_types=(real.type,),
)


Gate = Dialect(
"gate",
[
ConstantGateOp,
QuaternionGateOp,
],
[
AngleAttr,
Expand Down
7 changes: 7 additions & 0 deletions tests/filecheck/dialects/gate.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@

// CHECK-NEXT: %{{.*}} = gate.constant #gate.cz
// CHECK-GENERIC-NEXT: %{{.*}} = "gate.constant"() <{"gate" = #gate.cz}> : () -> !gate.type<2>

%zero = arith.constant 0 : i64
%one = arith.constant 1 : i64

// CHECK: %{{.*}} = gate.quaternion<i64> %zero + %one i + %zero j + %zero k
// CHECK-GENERIC: %{{.*}} = "gate.quaternion"(%zero, %one, %zero, %zero) : (i64, i64, i64, i64) -> !gate.type<1>
%2 = gate.quaternion<i64> %zero + %one i + %zero j + %zero k

0 comments on commit 73eaa4c

Please sign in to comment.