microsoft/qsharp-runtime

Trace throws exception on controlled operation

adrianleh opened this issue · 1 comments

Describe the bug
Trace Simulator throws an exception on a controlled operation

To Reproduce
Create an iqsharp python notebook with the following cells:

import os
os.environ["IQSHARP_LOG_LEVEL"] = "Debug"
import iqsharp
%%qsharp
operation Test(n : Int) : Unit {
    use ctls = Qubit[n - 1];
    use target = Qubit();
    for i in 0..Length(ctls) - 1 {
        Controlled (Ry(3.14 / 2.0, _)) ([ctls[i]], target);
    } 
}
Test.trace(n = 2)

Run the notebook cells in order from the browser as trace only works from the browser.

Trace will fail with the error shown under screenshots. The following exception will be printed to console:

System.AggregateException: One or more errors occurred. (Unable to cast object of type 'System.ValueTuple`2[System.Double,Microsoft.Quantum.Simulation.Core.Qubit]' to type 'Microsoft.Quantum.Simulation.Core.Qubit'.)
       ---> System.InvalidCastException: Unable to cast object of type 'System.ValueTuple`2[System.Double,Microsoft.Quantum.Simulation.Core.Qubit]' to type 'Microsoft.Quantum.Simulation.Core.Qubit'.
         at Microsoft.Quantum.Simulation.Core.ControlledOperation`2.In.get_Value() in D:\a\1\s\submodules\qsharp-runtime\src\Simulation\Core\Operations\Controlled.cs:line 58
         at Microsoft.Quantum.Simulation.Core.ControlledOperation`2.GetRuntimeMetadata(IApplyData args) in D:\a\1\s\submodules\qsharp-runtime\src\Simulation\Core\Operations\Controlled.cs:line 121
         at Microsoft.Quantum.IQSharp.ExecutionPathTracer.ExecutionPathTracer.OnOperationStartHandler(ICallable operation, IApplyData arguments) in D:\a\1\s\submodules\iqsharp\src\ExecutionPathTracer\ExecutionPathTracer.cs:line 51
         at Microsoft.Quantum.Simulation.Core.Operation`2.Apply(I a)
         at Microsoft.Quantum.Simulation.Core.Operation`2.Microsoft.Quantum.Simulation.Core.ICallable<I,O>.Apply(I args)
         at Microsoft.Quantum.Simulation.Core.Operation`2.Apply(I a)
         at Microsoft.Quantum.Simulation.Core.Operation`2.Microsoft.Quantum.Simulation.Core.ICallable<I,O>.Apply(I args)
         at SNIPPET.Test.<get___Body__>b__39_0(ValueTuple`2 __in__)
         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) in D:\a\1\s\submodules\qsharp-runtime\src\Simulation\Simulators\CommonNativeSimulator\SimulatorBase.cs:line 170
         at Microsoft.Quantum.Simulation.Common.SimulatorBase.<>c__DisplayClass68_0`3.<Run>b__0() in D:\a\1\s\submodules\qsharp-runtime\src\Simulation\Simulators\CommonNativeSimulator\SimulatorBase.cs:line 188
         at System.Threading.Tasks.Task`1.InnerInvoke()
         at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
         at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
      --- End of stack trace from previous location ---
         at CallSite.Target(Closure , CallSite , Object )
         at Microsoft.Quantum.IQSharp.OperationInfoExtensions.RunAsync(OperationInfo op, IOperationFactory qsim, IDictionary`2 arguments) in D:\a\1\s\submodules\iqsharp\src\Core\OperationInfo.cs:line 84
         at Microsoft.Quantum.IQSharp.Kernel.TraceMagic.RunAsync(String input, IChannel channel) in D:\a\1\s\submodules\iqsharp\src\Kernel\Magic\TraceMagic.cs:line 186
         --- End of inner exception stack trace ---
         at System.Threading.Tasks.Task`1.get_Result()
         at Microsoft.Quantum.IQSharp.Kernel.TraceMagic.Run(String input, IChannel channel) in D:\a\1\s\submodules\iqsharp\src\Kernel\Magic\TraceMagic.cs:line 147
         at Microsoft.Quantum.IQSharp.Jupyter.AbstractMagic.RunCancellable(String input, IChannel channel, CancellationToken cancellationToken) in D:\a\1\s\submodules\iqsharp\src\Jupyter\Magic\AbstractMagic.cs:line 197
         at Microsoft.Quantum.IQSharp.Jupyter.AbstractMagic.<>c__DisplayClass2_1.<SafeExecute>b__1() in D:\a\1\s\submodules\iqsharp\src\Jupyter\Magic\AbstractMagic.cs:line 56
         at System.Threading.Tasks.Task`1.InnerInvoke()
         at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
      --- End of stack trace from previous location ---
         at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
      --- End of stack trace from previous location ---
         at Microsoft.Quantum.IQSharp.Jupyter.AbstractMagic.<>c__DisplayClass2_0.<<SafeExecute>b__0>d.MoveNext() in D:\a\1\s\submodules\iqsharp\src\Jupyter\Magic\AbstractMagic.cs:line 56

Expected behavior
The circuit created by test would be visualized

Screenshots

image

System information

  • OS: Windows 10
  • Browser: Edge
  • Version: 0.25.222597

Thanks for reporting this! I'll go on and transfer this to the qsharp-runtime repo, as this looks to be a bug with the simulator itself.