Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Changes from all commits
Commits
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
53 changes: 37 additions & 16 deletions src/QsCompiler/Compiler/RewriteSteps/ClassicallyControlled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.Quantum.QsCompiler.DataTypes;
using Microsoft.Quantum.QsCompiler.SyntaxTree;
using Microsoft.Quantum.QsCompiler.Transformations.ClassicallyControlled;

Expand Down Expand Up @@ -30,20 +32,7 @@ public ClassicallyControlled()

public bool PreconditionVerification(QsCompilation compilation)
{
var controlNs = compilation.Namespaces
.FirstOrDefault(ns => ns.Name.Equals(BuiltIn.ClassicallyControlledNamespace));

if (controlNs == null)
{
return false;
}

var providedOperations = new QsNamespace[] { controlNs }
.Callables()
.Select(c => c.FullName)
.ToHashSet();
var requiredBuiltIns = new HashSet<QsQualifiedName>()
{
var classicallyControlledRequired = ImmutableHashSet.Create(
BuiltIn.ApplyIfZero.FullName,
BuiltIn.ApplyIfZeroA.FullName,
BuiltIn.ApplyIfZeroC.FullName,
Expand All @@ -58,9 +47,23 @@ public bool PreconditionVerification(QsCompilation compilation)
BuiltIn.ApplyIfElseRA.FullName,
BuiltIn.ApplyIfElseRC.FullName,
BuiltIn.ApplyIfElseRCA.FullName
};
);

return requiredBuiltIns.IsSubsetOf(providedOperations);
if (!CheckForRequired(compilation, BuiltIn.ClassicallyControlledNamespace, classicallyControlledRequired))
{
return false;
}

var cannonRequired = ImmutableHashSet.Create(
BuiltIn.NoOp.FullName
);

if (!CheckForRequired(compilation, BuiltIn.CanonNamespace, cannonRequired))
{
return false;
}

return true;
}

public bool Transformation(QsCompilation compilation, out QsCompilation transformed)
Expand All @@ -73,5 +76,23 @@ public bool PostconditionVerification(QsCompilation compilation)
{
throw new System.NotImplementedException();
}

private bool CheckForRequired(QsCompilation compilation, NonNullable<string> namespaceName, ImmutableHashSet<QsQualifiedName> requiredBuiltIns)
{
var builtInNs = compilation.Namespaces
.FirstOrDefault(ns => ns.Name.Equals(namespaceName));

if (builtInNs == null)
{
return false;
}

var providedOperations = new QsNamespace[] { builtInNs }
.Callables()
.Select(c => c.FullName)
.ToImmutableHashSet();

return requiredBuiltIns.IsSubsetOf(providedOperations);
}
}
}