diff --git a/MachineLearning/src/Structure.qs b/MachineLearning/src/Structure.qs index b76f1e00663..b6bf2503339 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 diff --git a/MachineLearning/tests/StructureTests.qs b/MachineLearning/tests/StructureTests.qs index 94802e9f237..7187e8da9db 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.");