From 0d521bb9da4fcf3779e9e3e3f62ef65147c63599 Mon Sep 17 00:00:00 2001 From: Hyunjin Song Date: Sun, 21 Jun 2020 11:45:03 +0900 Subject: [PATCH] Fix overestimation in MachineLearning.NQubitsRequired --- MachineLearning/src/Structure.qs | 15 ++++++--------- MachineLearning/tests/StructureTests.qs | 3 ++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/MachineLearning/src/Structure.qs b/MachineLearning/src/Structure.qs index a3a80c5abd1..1f80528c3e9 100644 --- a/MachineLearning/src/Structure.qs +++ b/MachineLearning/src/Structure.qs @@ -20,17 +20,14 @@ namespace Microsoft.Quantum.MachineLearning { /// may be applied. function NQubitsRequired(model : SequentialModel) : Int { - mutable nQubitsRequired = 0; + mutable lastQubitIndex = -1; // Need to return 0 if there are no gates for (gate in model::Structure) { - set nQubitsRequired = 1 + Fold( - MaxI, 0, - gate::ControlIndices + [ - gate::TargetIndex, - nQubitsRequired - ] + set lastQubitIndex = Fold( + MaxI, lastQubitIndex, + gate::ControlIndices + [gate::TargetIndex] ); } - return nQubitsRequired; + return lastQubitIndex + 1; } /// # Summary @@ -210,4 +207,4 @@ namespace Microsoft.Quantum.MachineLearning { return combined; } -} \ No newline at end of file +} diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs index e9ee25a29ae..e9ea8618386 100644 --- a/MachineLearning/tests/StructureTests.qs +++ b/MachineLearning/tests/StructureTests.qs @@ -12,7 +12,8 @@ namespace Microsoft.Quantum.MachineLearning.Tests { function NQubitsRequiredFact() : Unit { let model = Default() w/ Structure <- [ - ML.ControlledRotation((3, [7, 9]), PauliX, 0) + ML.ControlledRotation((3, [7, 9]), PauliX, 0), + ML.ControlledRotation((8, new Int[0]), PauliY, 1) ]; let actual = ML.NQubitsRequired(model); EqualityFactI(actual, 10, "Wrong output from NQubitsRequired.");