From ab799a8f60f69317a57c91e8776267eafe4e87d3 Mon Sep 17 00:00:00 2001 From: Raphael Koh Date: Thu, 6 Aug 2020 12:33:15 -0400 Subject: [PATCH 1/3] Remove duplicate qubits from RuntimeMetadata.Targets --- src/Simulation/Core/Operations/Operation.cs | 23 ++++++++++------- .../Circuits/RuntimeMetadataTest.qs | 4 +++ .../Simulators.Tests/RuntimeMetadataTests.cs | 25 +++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Simulation/Core/Operations/Operation.cs b/src/Simulation/Core/Operations/Operation.cs index aba000d7765..8cd8080abf1 100644 --- a/src/Simulation/Core/Operations/Operation.cs +++ b/src/Simulation/Core/Operations/Operation.cs @@ -16,7 +16,7 @@ public partial interface ICallable : ICallable { O Apply(I args); - ICallable Partial

(Func mapper); + ICallable Partial

(Func mapper); } ///

@@ -34,7 +34,7 @@ public interface IOperationWrapper /// /// Type of input parameters. /// Type of return values. - [DebuggerTypeProxy(typeof(Operation<,>.DebuggerProxy))] + [DebuggerTypeProxy(typeof(Operation<,>.DebuggerProxy))] public abstract class Operation : AbstractCallable, ICallable { private Lazy> _adjoint; @@ -56,7 +56,7 @@ public Operation(IOperationFactory m) : base(m) public virtual IApplyData __dataIn(I data) => new QTuple(data); - + public virtual IApplyData __dataOut(O data) => new QTuple(data); [DebuggerBrowsable(DebuggerBrowsableState.Never)] @@ -78,13 +78,18 @@ public Operation(IOperationFactory m) : base(m) public ControlledOperation Controlled => _controlled.Value; /// - public override RuntimeMetadata? GetRuntimeMetadata(IApplyData args) => - new RuntimeMetadata() + public override RuntimeMetadata? GetRuntimeMetadata(IApplyData args) + { + var targets = args.GetQubits() ?? new List(); + // Remove duplicate qubits + targets = new HashSet(targets).ToList(); + return new RuntimeMetadata() { Label = ((ICallable)this).Name, FormattedNonQubitArgs = args.GetNonQubitArgumentsAsString() ?? "", - Targets = args.GetQubits() ?? new List(), + Targets = targets, }; + } public O Apply(I a) { @@ -95,7 +100,7 @@ public O Apply(I a) this.Factory?.StartOperation(this, __dataIn(a)); __result__ = this.Body(a); } - catch( Exception e) + catch (Exception e) { this.Factory?.Fail(System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(e)); throw; @@ -105,7 +110,7 @@ public O Apply(I a) this.Factory?.EndOperation(this, __dataOut(__result__)); } - return __result__; + return __result__; } public T Partial(object partialInfo) @@ -212,7 +217,7 @@ internal class DebuggerProxy { private Operation op; - public DebuggerProxy(Operation op) + public DebuggerProxy(Operation op) { this.op = op; } diff --git a/src/Simulation/Simulators.Tests/Circuits/RuntimeMetadataTest.qs b/src/Simulation/Simulators.Tests/Circuits/RuntimeMetadataTest.qs index b75e70fb7d5..4e721723832 100644 --- a/src/Simulation/Simulators.Tests/Circuits/RuntimeMetadataTest.qs +++ b/src/Simulation/Simulators.Tests/Circuits/RuntimeMetadataTest.qs @@ -26,5 +26,9 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests.Circuits { HOp(q); } } + + operation TwoQubitOp (q1 : Qubit, q2 : Qubit) : Unit { + // ... + } } diff --git a/src/Simulation/Simulators.Tests/RuntimeMetadataTests.cs b/src/Simulation/Simulators.Tests/RuntimeMetadataTests.cs index a31849fe169..e9f6e69e626 100644 --- a/src/Simulation/Simulators.Tests/RuntimeMetadataTests.cs +++ b/src/Simulation/Simulators.Tests/RuntimeMetadataTests.cs @@ -409,7 +409,10 @@ public void MResetZ() Assert.Equal(op.GetRuntimeMetadata(args), expected); } + } + public class CustomCircuitTests + { [Fact] public void EmptyOperation() { @@ -475,6 +478,28 @@ public void NestedOperation() Assert.Equal(op.GetRuntimeMetadata(args), expected); } + + [Fact] + public void DuplicateQubitArgs() + { + var q = new FreeQubit(0); + var op = new QuantumSimulator().Get(); + var args = op.__dataIn((q, q)); + var expected = new RuntimeMetadata() + { + Label = "TwoQubitOp", + FormattedNonQubitArgs = "", + IsAdjoint = false, + IsControlled = false, + IsMeasurement = false, + IsComposite = false, + Children = null, + Controls = new List() { }, + Targets = new List() { q }, + }; + + Assert.Equal(op.GetRuntimeMetadata(args), expected); + } } public class UDTTests From 397c3b20fc632e40b12d1012d9db54e992dac2b3 Mon Sep 17 00:00:00 2001 From: Raphael Koh Date: Thu, 6 Aug 2020 13:17:11 -0400 Subject: [PATCH 2/3] Use Distinct --- src/Simulation/Core/Operations/Operation.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Simulation/Core/Operations/Operation.cs b/src/Simulation/Core/Operations/Operation.cs index 8cd8080abf1..d9648cc3c8e 100644 --- a/src/Simulation/Core/Operations/Operation.cs +++ b/src/Simulation/Core/Operations/Operation.cs @@ -78,18 +78,13 @@ public Operation(IOperationFactory m) : base(m) public ControlledOperation Controlled => _controlled.Value; /// - public override RuntimeMetadata? GetRuntimeMetadata(IApplyData args) - { - var targets = args.GetQubits() ?? new List(); - // Remove duplicate qubits - targets = new HashSet(targets).ToList(); - return new RuntimeMetadata() + public override RuntimeMetadata? GetRuntimeMetadata(IApplyData args) => + new RuntimeMetadata() { Label = ((ICallable)this).Name, FormattedNonQubitArgs = args.GetNonQubitArgumentsAsString() ?? "", - Targets = targets, + Targets = args.GetQubits().Distinct() ?? new List(), }; - } public O Apply(I a) { From 4fd98c9002756939886cccc41ebdc0e91b1968a7 Mon Sep 17 00:00:00 2001 From: Raphael Koh Date: Thu, 6 Aug 2020 13:35:41 -0400 Subject: [PATCH 3/3] Add null check --- src/Simulation/Core/Operations/Operation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simulation/Core/Operations/Operation.cs b/src/Simulation/Core/Operations/Operation.cs index d9648cc3c8e..6a20d69af6c 100644 --- a/src/Simulation/Core/Operations/Operation.cs +++ b/src/Simulation/Core/Operations/Operation.cs @@ -83,7 +83,7 @@ public Operation(IOperationFactory m) : base(m) { Label = ((ICallable)this).Name, FormattedNonQubitArgs = args.GetNonQubitArgumentsAsString() ?? "", - Targets = args.GetQubits().Distinct() ?? new List(), + Targets = args.GetQubits()?.Distinct() ?? new List(), }; public O Apply(I a)