Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.
/ qsharp-unity Public archive

Q# <-> Unity Prototype

License

Notifications You must be signed in to change notification settings

amirebrahimi/qsharp-unity

Repository files navigation

Q# <-> Unity

Background

Someone mentioned at a QC meetup that it would be cool to see Q# and Unity work together. So, I figured it was worth a quick-and-dirty prototype. I did this work back in October 2020 and never released it because it doesn't have a sustainable workflow (more on that later). After someone tagged me in a tweet I figured it would be worth re-visiting:

Hello @amir_e!
Would you happend to have any insight about "How could you use Q# within Unity?"
➡️ https://t.co/mmE3yB7Ix3

— LittleCoinCoin (@LittleCoinCoin1) March 9, 2021

Even then, I just finally got around to getting everything pushed to github. So, included in this repo is a Unity project that will allow you to write some code like this:

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    async void Start()
    {
        UnitySystemConsoleRedirector.Redirect();

        var bits = new[] { false, true, false };
        using (var sim = new QuantumSimulator())
        {
            Console.WriteLine($"Input: {bits.ToDelimitedString()}");

            var restored = await RunAlgorithm.Run(sim, new QArray<bool>(bits));
            Console.WriteLine($"Output: {restored.ToDelimitedString()}");

            Debug.Assert(bits.Parity() == restored.Parity());
        }
    }
}

and see output like this in the Console window: Console output

It makes use of a Q# program (i.e. RunAlgorithm) that must be compiled separately (if you want to modify) and copied to the Packages/host/ directory. However, this step isn't necessary if you just want to run the above and test. I have already checked in a built copy of the assembly into the repository.

Workflow

Requires:

For the above, I think the easiest thing to do is open up the quantum-dotnet.sln project and see what Visual Studio complains about (e.g. it may not even load the projects because you're missing something). Install those things and then you'll probably find more things that are complained about (e.g. missing QDK components). I ended up doing the following from a VS developer command prompt after seeing some warnings in Visual Studio:

dotnet restore quantum-dotnet.sln

I think this downloads missing dependencies, but I'm not entirely sure. Unfortunately, I still got an error with trying to build this again after the time that has passed (I'm on a new machine with a fresh install of Windows):

Error	QS7016	Unable to load the file "C:\...\qsharp-runtime\src\Simulation\CsharpGeneration\bin\Debug\netstandard2.0\Microsoft.Quantum.CsharpGeneration.dll" specifying transformations to perform as part of the compilation process. Unable to load one or more of the requested types.

Perhaps you won't get this error, but I don't have any interest in investigating further for the following reasons:

  1. A custom fork of qsharp-compiler and qsharp-runtime were required in order to compile under netstandard2.0. Unity unfortunately didn't support netstandard2.1 yet (at the time I did this work) and it wasn't clear yet on when it would be supported. This was not an easy feat to get it to compile and I have no interest in maintaining a fork of those two projects.
  2. So much of the pipeline between qsharp-compiler and qsharp-runtime relies on them working in tandem, so I had to manually compile .qs files in order to complete the build process for those custom forks.
  3. Generated files require a manual copy into Unity along with their dependencies.

This brings me to my final point -- I'm not going to support any of this because it is just too much work for a single person. You'll notice that I've removed the Issues feature from GitHub and will archive the repository (as read-only) after committing everything. My guess is that something will break along the way if you are trying to re-trace my steps. I simply figured I'd provide this to those of you who are interested about integration and looking for a jumping-off point.

The good news is that as I write this up and look back at the netstandard2.1 issue I mentioned above I see that Unity is just now releasing experimental support in 2021.2.0a8. Perhaps give that a shot and see if you have better luck with getting Q# programs into Unity. Maybe some of what I have shared here can help.