Hack around recursive operations calls
Describe the bug
When calling recursive operations in Base profile you get an error (this the correct behavior).
But you can get around it using the following hack. And it runs.
namespace TopLevel {
@EntryPoint()
operation Main() : Unit {
RecursiveOperation(0);
}
operation RecursiveOperation(x: Int) : Unit {
use q = Qubit();
Message($"Depth {x}");
let hack = [RecursiveOperation];
hack[0](x + 1);
}
}
To Reproduce
Steps to reproduce the behavior:
- Open the playground on the latest main branch.
- Paste the source code provided above.
- Change the profile to Base.
- Click on Run.
- You will see that the code runs and prints some Messages.
- Now go to the QIR tab, you will see "Error: memory access out of bounds".
Expected behavior
Describe what you expect to happen, versus what actually happened.
Screenshots
Correct behavior
Hack
System information
- Version of the package or extension for which the problem occurs: latest main branch
- Your operating system: Windows 11
- For issues with extensions, the version of the IDE for which the problem occurs: Playground
This one is tricky to address... RCA doesn't see this as recursive because there is a use of a runtime-resolved callable. Then at partial eval it will try to evaluate the call, get stuck in the infinite recursion, and fail with out of memory. On the one hand, it would be great if it could recognize and avoid this. On the other hand, it's no different than a classical infinite loop or other program that fails to terminate for classical reasons. The cycle check in RCA is best effort, but at the end of the day this seems to reduce to yet another version of the halting problem.