-
Notifications
You must be signed in to change notification settings - Fork 90
Simulator should allow release of measured Qubits #206
Description
** Please describe what you would like the feature to accomplish.**
The current simulator implementation requires that all qubits be reset to the |0> state before release, and treats it as a runtime error if this is not the case. The reset can be accomplished via an actual call to Microsoft.Quantum.Intrinsic.Reset or via other appropriate operations that put the qubit into the |0> state. The intent is to make sure no quantum state is "leaked" by releasing a qubit that may still be carrying information via superposition or entanglement. However, this goal can also be met by allowing release of a qubit that has been measured with regard to any single basis and then had no further operations performed on it.
** Describe the solution you'd like **
The simulator check for qubits on release should be updated to allow release of a qubit if it is |0> or if it has been measured with regard to any basis.
** Describe alternatives you've considered **
An alternative could be removing the simulator check for qubit state on release entirely. However, that would be undesirable given the benefit for algorithm validation that this check affords. Without any check, it would be too easy to write algorithms in Q# that did not correctly account for cleaning up qubits that might contain relevant program state.
** Additional context **
The following sample code demonstrates the behavior:
namespace Test {
open Microsoft.Quantum.Intrinsic;
@EntryPoint()
operation Execute() : Result {
using (q = Qubit()) {
H(q);
return M(q);
}
}
}
Because the M will result in collapsing the state to Zero about half the time, this code will execute successfully half of the time but fail with the following error the rest of the time:
Unhandled exception. Microsoft.Quantum.Simulation.Simulators.Exceptions.ReleasedQubitsAreNotInZeroState: Released qubits are not in zero state.
---> Test.Execute on C:\Users\swern\Programming\experimental\Wgate\Program.qs:line 8
Unhandled exception: Microsoft.Quantum.Simulation.Simulators.Exceptions.ReleasedQubitsAreNotInZeroState: Released qubits are not in zero state.
at Microsoft.Quantum.Simulation.Simulators.QuantumSimulator.QSimQubitManager.ReleaseOneQubit(Qubit qubit, Boolean usedOnlyForBorrowing)
at Microsoft.Quantum.Simulation.Common.QubitManager.Release(Qubit qubit)
at Microsoft.Quantum.Simulation.Common.SimulatorBase.Release.Apply(Qubit q)
at Test.Execute.<get_Body>b__24_0(QVoid __in__) in C:\Users\swern\Programming\experimental\Wgate\Program.qs:line 8
at Microsoft.Quantum.Simulation.Core.Operation`2.Apply(I a)
at Microsoft.Quantum.Simulation.Core.Operation`2.Apply[GenO](Object args)
at Microsoft.Quantum.Simulation.Common.SimulatorBase.Execute[T,I,O](I args)
at Microsoft.Quantum.Simulation.Common.SimulatorBase.<>c__DisplayClass43_0`3.<Run>b__0()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object
obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception
was thrown ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception
was thrown ---
at Test.__QsEntryPoint__.EntryPoint.Run(IOperationFactory __factory__) in C:\Users\swern\Programming\experimental\Wgate\obj\qsharp\src\Program.EntryPoint.g.cs:line 29
at Test.__QsEntryPoint__.Driver.DisplayEntryPointResult(EntryPoint entryPoint, Func`1 createSimulator) in C:\Users\swern\Programming\experimental\Wgate\obj\qsharp\src\Program.EntryPoint.g.cs:line 153
at Test.__QsEntryPoint__.Driver.Simulate(EntryPoint entryPoint, String simulator) in C:\Users\swern\Programming\experimental\Wgate\obj\qsharp\src\Program.EntryPoint.g.cs:line 136
at System.CommandLine.Invocation.CommandHandler.GetResultCodeAsync(Object value, InvocationContext context)
at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseErrorReporting>b__20_0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass15_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass24_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__21_0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseDirective>b__19_0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseDebugDirective>b__11_0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location where exception
was thrown ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass13_0.<<UseExceptionHandler>b__0>d.MoveNext()
Note that replacing the H with an X will result in hitting this failure consistently, as expected. With the suggested change, both the H version and the X version would succeed consistently.