-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow vector patterns to be used in kernels #38
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -866,9 +866,9 @@ detectVecErrors :: UserName -- Term constructor name | |
-> Checking (Error -> Error) -- Returns error wrapper to use for recursion | ||
detectVecErrors vcon (PrefixName [] "Vec") [_, VNum n] [_, VPNum p] ty tp = | ||
case numMatch B0 n p of | ||
Left (NumMatchFail _ _) -> do | ||
p' <- toLenConstr p | ||
err $ getVecErr tp (show ty) (show n) p' | ||
Left (NumMatchFail _ _) -> case (toLenConstr p) of | ||
Right p' -> err $ getVecErr tp (show ty) (show n) p' | ||
Left p' -> err $ InternalError ("detectVecErrors: Unexpected pattern: " ++ show (toLenConstr p')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this I'm hoping this should/could just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, it's a bit hard to follow as is |
||
-- Even if we succed here, the error might pop up when checking the | ||
-- rest of the vector. We return a function here that intercepts the | ||
-- error and extends it to the whole vector. | ||
|
@@ -877,13 +877,14 @@ detectVecErrors vcon (PrefixName [] "Vec") [_, VNum n] [_, VPNum p] ty tp = | |
pure (consError fc tp (show ty) n) | ||
else pure id | ||
where | ||
-- For constructors that produce something of type Vec we should | ||
-- only ever get the patterns NP0 (if vcon == PrefixName [] "nil") | ||
-- and NP1Plus (if vcon == PrefixName [] "cons") | ||
toLenConstr :: NumPat -> Checking LengthConstraint | ||
-- Try to work out the length of a vector | ||
-- We only want to know if the vector length is nil or a successor | ||
toLenConstr :: NumPat -> Either NumPat (LengthConstraint) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Superfluous extra brackets here, at least |
||
toLenConstr NP0 = pure $ Length 0 | ||
toLenConstr (NP1Plus (NP2Times np)) = either (const (Right LengthOdd)) Right (toLenConstr np) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok...so....what does this do.....
Is that right? (This is quite hard to follow) If so, how about |
||
toLenConstr (NP1Plus _) = pure $ LongerThan 0 | ||
toLenConstr p = err $ InternalError ("detectVecErrors: Unexpected pattern: " ++ show p) | ||
toLenConstr (NP2Times np) = either (const (Right LengthEven)) Right (toLenConstr np) | ||
toLenConstr p = Left p | ||
detectVecErrors _ _ _ _ _ _ = pure id | ||
|
||
getVecErr :: Either (Term d k) Pattern -> (String -> String -> LengthConstraint -> ErrorMsg) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,28 @@ kernelConstructors = M.fromList | |
(RPr ("tail", TVec (VApp (VInx (VS VZ)) B0) (VNum $ nVar (VInx VZ))) | ||
(RPr ("head", VApp (VInx (VS VZ)) B0) R0))) | ||
]) | ||
,(CConcatEqEven, M.fromList | ||
[(CVec, CArgs [VPVar, VPNum (NP2Times NPVar)] (Sy (Sy Zy)) | ||
-- Star should be a TypeFor m forall m? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really sure I understand this comment...what happens if you replace the star with that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try and push something to clear this up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My idea was that you could split on classical variables in the kernel, like
but having just tried that, there are more blockers ( |
||
(REx ("elementType", Star []) ((REx ("halfLength", Nat) (R0)))) | ||
(RPr ("lhs", TVec (VApp (VInx (VS VZ)) B0) (VApp (VInx VZ) B0)) | ||
(RPr ("rhs", TVec (VApp (VInx (VS VZ)) B0) (VApp (VInx VZ) B0)) R0))) | ||
]) | ||
,(CRiffle, M.fromList | ||
[(CVec, CArgs [VPVar, VPNum (NP2Times NPVar)] (Sy (Sy Zy)) | ||
-- Star should be a TypeFor m forall m? | ||
(REx ("elementType", Star []) ((REx ("halfLength", Nat) (R0)))) | ||
(RPr ("evens", TVec (VApp (VInx (VS VZ)) B0) (VApp (VInx VZ) B0)) | ||
(RPr ("odds", TVec (VApp (VInx (VS VZ)) B0) (VApp (VInx VZ) B0)) R0))) | ||
]) | ||
,(CConcatEqOdd, M.fromList | ||
[(CVec, CArgs [VPVar, VPNum (NP1Plus (NP2Times NPVar))] (Sy (Sy Zy)) | ||
-- Star should be a TypeFor m forall m? | ||
(REx ("elementType", Star []) ((REx ("halfLength", Nat) (R0)))) | ||
(RPr ("lhs", TVec (VApp (VInx (VS VZ)) B0) (VApp (VInx VZ) B0)) | ||
(RPr ("mid", VApp (VInx (VS VZ)) B0) | ||
(RPr ("rhs", TVec (VApp (VInx (VS VZ)) B0) (VApp (VInx VZ) B0)) R0)))) | ||
]) | ||
,(CTrue, M.fromList [(CBit, CArgs [] Zy R0 R0)]) | ||
,(CFalse, M.fromList [(CBit, CArgs [] Zy R0 R0)]) | ||
] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- Create a "brickwork" state | ||
-- Apply a parameterised unitary U to entangle every adjacent pair of qubits in a line architecture. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eeek - skipping dependent types to ensure weights match in length, is this roughly just If so then whilst a fun bit of list comprehensions they don't really do much useful (you're just riffling things apart and back together, and so on), right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right, it's just a bit of fun that tests vector patterns are working in kernels 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Terminology-wise, I think this is called a brickwork pattern and a ladder is when everything else is sequentially entangled with 1 qubit |
||
-- (don't apply the gate to (q0, qn-1)) | ||
brick(n :: #, -- The number of entangling gates | ||
Vec(Float, n), -- Angles for each of the gates | ||
U :: { Float -> { Qubit, Qubit -o Qubit, Qubit }}) -- Parameterised unitary | ||
-> { Vec(Qubit, n + 1) -o Vec(Qubit, n + 1) } | ||
brick(0, [], _) = { .. } | ||
-- Odd number of gates, even number of qubits | ||
brick(succ(doub(n)), th ,- ths, U) = { | ||
(q0 ,- qsEven) =%= (q1 ,- qsOdd) => | ||
let q0, q1 = U(th)(q0, q1) in | ||
q0 ,- brick(doub(n), ths, U)(q1 ,- (qsEven =%= qsOdd)) | ||
} | ||
-- Even number of gates, odd number of qubits | ||
brick(doub(succ(n)), th ,- ths, U) = { (q0 ,- qsl) =, qmid ,= qsr => | ||
let q1 ,- qs = brick(succ(doub(n)), ths, U)((qsl -, qmid) =,= qsr) in | ||
let q0, q1 = U(th)(q0, q1) in | ||
q0 ,- q1 ,- qs | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
f(X :: *, n :: #, Vec(X, n)) -> Vec(X, n) | ||
f(X, n, xs) = xs | ||
|
||
g :: Vec(Nat, 3) | ||
g = f(Nat, 3, [1] =,= [2]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Error in test/golden/error/even-length.brat@FC {start = Pos {line = 5, col = 15}, end = Pos {line = 5, col = 26}}: | ||
g = f(Nat, 3, [1] =,= [2]) | ||
^^^^^^^^^^^ | ||
|
||
Expected vector of length 3 | ||
from the type: Vec(VApp VPar In checking_check_defs_1_g_1_ 0 B0, VPar In checking_check_defs_1_g_1_ 1) | ||
but got vector: concatEqEven([1], [2]) | ||
of even length | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
f(X :: *, n :: #, Vec(X, n)) -> Vec(X, n) | ||
f(X, n, xs) = xs | ||
|
||
g :: Vec(Nat, 4) | ||
g = f(Nat, 4, [1] =, 2 ,= [3]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Error in test/golden/error/odd-length.brat@FC {start = Pos {line = 5, col = 15}, end = Pos {line = 5, col = 30}}: | ||
g = f(Nat, 4, [1] =, 2 ,= [3]) | ||
^^^^^^^^^^^^^^^ | ||
|
||
Expected vector of length 4 | ||
from the type: Vec(VApp VPar In checking_check_defs_1_g_1_ 0 B0, VPar In checking_check_defs_1_g_1_ 1) | ||
but got vector: concatEqOdd([1], 2, [3]) | ||
of odd length | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So whilst continuing the slightly-hacked-in nature here and a better framework might be nice I don't think this is all that bad, certainly I'm not objecting to patching it in like this...