Skip to content

Commit

Permalink
Fix modifier at U
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Jan 13, 2025
1 parent 1bb218f commit 1724b2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
2 changes: 2 additions & 0 deletions visitor/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package visitor

import "math"

const U string = "U"

var Const = map[string]float64{
"pi": math.Pi,
"tau": 2 * math.Pi,
Expand Down
34 changes: 11 additions & 23 deletions visitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,41 +312,26 @@ func (v *Visitor) Params(xlist parser.IExpressionListContext) ([]float64, error)
}

func (v *Visitor) Modify(u matrix.Matrix, qargs []q.Qubit, modifier []parser.IGateModifierContext) (matrix.Matrix, error) {
var ctrl, negctrl []q.Qubit
for i, mod := range modifier {
// https://openqasm.com/language/gates.html#inverse-modifier
// The inverse of a controlled operation is defined by inverting the control unitary. That is, inv @ ctrl @ U = ctrl @ inv @ U.

n := v.Visit(mod).(int64)
switch {
case mod.CTRL() != nil:
ctrl = append(ctrl, qargs[i])
u = AddControlled(u, i)
case mod.NEGCTRL() != nil:
ctrl, negctrl = append(ctrl, qargs[i]), append(negctrl, qargs[i])
n := v.qsim.NumberOfBit()
x := gate.TensorProduct(gate.X(), n, []int{i})

u = AddControlled(u, i)
u = matrix.Apply(x, u, x)
case mod.INV() != nil:
u = u.Dagger()
case mod.POW() != nil:
// TODO: ctrl @ pow(0.5) @ U is not equal to pow(0.5) @ ctrl @ U
u = matrix.ApplyN(u, int(n))
default:
return nil, fmt.Errorf("modifier=%s: %w", mod.GetText(), ErrUnexpected)
}
}

switch len(ctrl) {
case 0:
n := v.qsim.NumberOfBit()
u = gate.TensorProduct(u, n, q.Index(qargs...))
default:
n := v.qsim.NumberOfBit()
u = gate.Controlled(u, n, q.Index(ctrl...), qargs[len(qargs)-1].Index())

if len(negctrl) > 0 {
x := gate.TensorProduct(gate.X(), n, q.Index(negctrl...))
u = matrix.Apply(x, u, x)
}
}

return u, nil
}

Expand All @@ -364,14 +349,17 @@ func (v *Visitor) Builtin(ctx *parser.GateCallStatementContext) (matrix.Matrix,

id := v.Visit(ctx.Identifier()).(string)
switch id {
case "U":
case U:
params, err := v.Params(ctx.ExpressionList())
if err != nil {
return nil, false, fmt.Errorf("params: %w", err)
}
qargs := v.Visit(ctx.GateOperandList()).([][]q.Qubit)

n := v.qsim.NumberOfBit()
u := gate.U(params[0], params[1], params[2])
u = gate.TensorProduct(u, n, q.Index(flatten(qargs)...))

qargs := v.Visit(ctx.GateOperandList()).([][]q.Qubit)
u, err = v.Modify(u, flatten(qargs), ctx.AllGateModifier())
if err != nil {
return nil, false, fmt.Errorf("modify: %w", err)
Expand Down

0 comments on commit 1724b2b

Please sign in to comment.