Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Support for Conjugations as Statements - Part II #114

Merged
merged 42 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
c5824a1
infrastructure for with
Jul 13, 2019
b4e164b
proper verification regarding what functors need to be supported for …
Jul 15, 2019
9fe1fe3
need to make the blocks PositionedBlocks rather than just scopes
Jul 15, 2019
3a9fe51
context verification for conjugate-statement
Jul 16, 2019
7a20660
building conjugate-statement when building the syntax tree
Jul 16, 2019
f473df1
adding transformation for inlining (i.e. eliminating) all conjugate-s…
Jul 17, 2019
9c5104b
adding an apply method
Jul 17, 2019
43ce9aa
splitting out the transformations for doing something with (ie. curre…
Jul 18, 2019
5214f8f
making conjugate-statement an error in functions, but keeping it in t…
Jul 18, 2019
c73a0f7
fixing a stackoverflow for large files
Jul 20, 2019
49f3821
transformation for replacing all local variables with unique names
Aug 1, 2019
860f180
regex test
Aug 1, 2019
3f8c346
now we just need to add the inlining of conjugate-statements to the c…
Aug 1, 2019
d304e42
adding the conjugate inlining as step to teh copmilation builder
Aug 1, 2019
42368da
some renaming
Aug 1, 2019
6c4ee14
doc comment
Aug 1, 2019
55db03f
getting updates from master
Aug 1, 2019
bd98d6c
adaptions due to merge
Aug 1, 2019
68bd6d3
we need to clean up our syntax coloring...
Aug 1, 2019
8f59b82
renaming the keywords and switching to order outer, inner
Aug 1, 2019
5e355e3
forgot some things
Aug 2, 2019
f39f18b
minor things
Aug 2, 2019
baeb604
some doc comments
Aug 2, 2019
9d7189f
Merge branch 'master' into beheim/withStatements
bettinaheim Aug 3, 2019
ac56695
renaming
Aug 3, 2019
89a3a88
outer scope needs to provide additional functor support
Aug 3, 2019
1ffc37f
correct colorization of "is"
Aug 3, 2019
97db968
adding the corresponding cases for conjugate-statements to the existi…
Aug 3, 2019
52122cd
final renaming
Aug 3, 2019
d030828
some clean up
Aug 3, 2019
b08fc8f
making getting unique variable names internal, since it overwrites ex…
Aug 3, 2019
f9b943b
typo
Aug 5, 2019
691e0d6
clarifying comments
Aug 6, 2019
94553fe
whitespace...
Aug 6, 2019
53ded6b
adding an a error for set statement within the apply-block as well as…
Aug 13, 2019
0e2f543
Doc comment
Aug 13, 2019
29883f7
proper transformation, now we just need to generate diagnostics
Aug 14, 2019
750d439
this should now properly give errors only when needed
Aug 14, 2019
f5d9fbd
Merge branch 'features/language' into beheim/conjugations
Aug 14, 2019
0b81d5d
minor thing
Aug 14, 2019
a2a3ea4
typo
Aug 14, 2019
47b1ed1
adding a couple of tests
Aug 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
forgot some things
  • Loading branch information
Bettina Heim committed Aug 2, 2019
commit 5e355e3d1df8106eabd6c775ffdf2e1a5f64c8f6
17 changes: 11 additions & 6 deletions src/QsCompiler/CompilationManager/TypeChecking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,10 +1409,10 @@ public static (LocalDeclarations, IEnumerable<QsStatement>) LocalDeclarationsAt(

LocalDeclarations Concat(LocalDeclarations fst, LocalDeclarations snd)
=> new LocalDeclarations(fst.Variables.Concat(snd.Variables).ToImmutableArray());
bool BeforePosition(QsStatement statement) =>
statement.Location.IsValue && DiagnosticTools.AsPosition(statement.Location.Item.Offset).IsSmallerThan(relativePosition);
bool BeforePosition(QsNullable<QsLocation> location) =>
location.IsValue && DiagnosticTools.AsPosition(location.Item.Offset).IsSmallerThan(relativePosition);

var precedingStatements = scope.Statements.TakeWhile(BeforePosition);
var precedingStatements = scope.Statements.TakeWhile(stm => BeforePosition(stm.Location));
if (!precedingStatements.Any()) return (scope.KnownSymbols, scope.Statements);
var lastPreceding = precedingStatements.Last();

Expand All @@ -1423,8 +1423,7 @@ bool BeforePosition(QsStatement statement) =>
var elseBlock = condStatement.Item.Default.ValueOr(null);
if (elseBlock != null) blocks = blocks.Concat(new[] { elseBlock });

var preceding = blocks.TakeWhile(block =>
block.Location.IsValue && DiagnosticTools.AsPosition(block.Location.Item.Offset).IsSmallerThan(relativePosition));
var preceding = blocks.TakeWhile(block => BeforePosition(block.Location));
relevantScope = preceding.Any() ? preceding.Last().Body : null;
}
if (lastPreceding.Statement is QsStatementKind.QsForStatement forStatement)
Expand All @@ -1436,12 +1435,18 @@ bool BeforePosition(QsStatement statement) =>
var allContainedStatements = repeatStatement.Item.RepeatBlock.Body.Statements.Concat(repeatStatement.Item.FixupBlock.Body.Statements).ToImmutableArray();
relevantScope = new QsScope(allContainedStatements, repeatStatement.Item.RepeatBlock.Body.KnownSymbols);
}
if (lastPreceding.Statement is QsStatementKind.QsConjugateStatement conjugateStatement)
{
relevantScope = BeforePosition(conjugateStatement.Item.InnerTransformation.Location)
? conjugateStatement.Item.InnerTransformation.Body
: conjugateStatement.Item.OuterTransformation.Body;
}
if (lastPreceding.Statement is QsStatementKind.QsQubitScope allocationScope)
{ relevantScope = allocationScope.Item.Body; }

if (relevantScope != null) return relevantScope.LocalDeclarationsAt(relativePosition);
var defined = precedingStatements.Aggregate(scope.KnownSymbols, (decl, statement) => Concat(decl, statement.SymbolDeclarations));
var followingStatements = scope.Statements.SkipWhile(BeforePosition);
var followingStatements = scope.Statements.SkipWhile(stm => BeforePosition(stm.Location));
return (defined, new[] { lastPreceding }.Concat(followingStatements));
}

Expand Down
4 changes: 2 additions & 2 deletions src/QsCompiler/SyntaxProcessor/StatementVerification.fs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ let NewRepeatStatement (symbols : SymbolTracker<_>) (repeatBlock : QsPositionedB
/// builds and returns the corresponding conjugate-statement representing the patter UVU* where U* is the adjoint of U.
/// Throws an ArgumentException if the given block specifying the inner transformation contains no location information.
let NewConjugateStatement (outer : QsPositionedBlock, inner : QsPositionedBlock) =
let location = inner.Location |> function
| Null -> ArgumentException "no location is set for the given block defining the transformation to conjugate" |> raise
let location = outer.Location |> function
| Null -> ArgumentException "no location is set for the given block defining the conjugating transformation" |> raise
| Value loc -> loc
QsConjugateStatement.New (outer, inner) |> QsConjugateStatement |> asStatement QsComments.Empty location []

Expand Down
17 changes: 15 additions & 2 deletions src/QsCompiler/SyntaxProcessor/TreeVerification.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ let AllPathsReturnValueOrFail body =
if not withinQubitScope then returnsWithinQubitScope.Clear()
| QsStatementKind.QsForStatement statement -> checkReturnStatements withinQubitScope statement.Body
| QsStatementKind.QsWhileStatement statement -> checkReturnStatements withinQubitScope statement.Body
| QsStatementKind.QsConjugateStatement statement ->
let added = statement.OuterTransformation.Body |> delayAddingReturns
checkReturnStatements withinQubitScope statement.InnerTransformation.Body
returnsWithinQubitScope.AddRange added
| QsStatementKind.QsRepeatStatement statement ->
let added = statement.RepeatBlock.Body |> delayAddingReturns
checkReturnStatements withinQubitScope statement.FixupBlock.Body
Expand All @@ -61,7 +65,10 @@ let AllPathsReturnValueOrFail body =
| Value block -> checkReturnStatements withinQubitScope block.Body
| Null -> ()
returnsWithinQubitScope.AddRange added
| _ -> ()
| QsStatementKind.QsExpressionStatement _
| QsStatementKind.QsFailStatement _
| QsStatementKind.QsValueUpdate _
| QsStatementKind.QsVariableDeclaration _ -> ()

// returns true if all paths in the given scope contain a terminating (i.e. return or fail) statement
let rec checkTermination (scope : QsScope) =
Expand All @@ -72,6 +79,9 @@ let AllPathsReturnValueOrFail body =
| QsStatementKind.QsForStatement _ // it is not immediately obvious whether or not the body will get executed, hence non-terminating
| QsStatementKind.QsWhileStatement _ -> true // same here
| QsStatementKind.QsQubitScope statement -> checkTermination statement.Body |> not
| QsStatementKind.QsConjugateStatement statement ->
checkTermination statement.OuterTransformation.Body |> not &&
checkTermination statement.InnerTransformation.Body |> not
| QsStatementKind.QsRepeatStatement statement ->
checkTermination statement.FixupBlock.Body |> ignore // only here to give warnings for unreachable code
checkTermination statement.RepeatBlock.Body |> not
Expand All @@ -80,7 +90,10 @@ let AllPathsReturnValueOrFail body =
match statement.Default with
| Value block -> checkTermination block.Body |> not || returns |> List.contains false
| Null -> true
| _ -> true
| QsStatementKind.QsExpressionStatement _
| QsStatementKind.QsFailStatement _
| QsStatementKind.QsValueUpdate _
| QsStatementKind.QsVariableDeclaration _ -> true

let returnOrFailAndAfter = Seq.toList <| scope.Statements.SkipWhile isNonTerminatingStatement
if returnOrFailAndAfter.Length <> 0 then
Expand Down
4 changes: 3 additions & 1 deletion src/QsCompiler/Transformations/CodeTransformations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public override QsNullable<QsLocation> onLocation(QsNullable<QsLocation> loc) =>
/// </summary>
public static QsScope GenerateAdjoint(this QsScope scope)
{
scope = new UniqueVariableNames().Transform(scope);
// Since we are pulling purely classical statements up, we are potentially changing the order of declarations.
// We therefore need to generate unique variable names before reordering the statements.
scope = new UniqueVariableNames().Transform(scope);
scope = ApplyFunctorToOperationCalls.ApplyAdjoint(scope);
scope = new ReverseOrderOfOperationCalls().Transform(scope);
return StripLocationInformation.Apply(scope);
Expand Down