QuantumKatas icon indicating copy to clipboard operation
QuantumKatas copied to clipboard

%kata magic doesn't recognize C# tests

Open tcNickolas opened this issue 6 years ago • 15 comments
trafficstars

PhaseEstimation kata has a test that covers Q# code but is written in C# due to the need to catch and process exceptions thrown by the task. When the kata is approached as Q# project, the test is recognized and executed together with Q# tests just fine. However, when the kata is converted to Jupyter Notebook format, the %kata magic command does not recognize the C# test. This is not unexpected, but ideally we need to come up with a way to execute C# tests from Q# notebooks.

This is related to one of the work items in #138.

tcNickolas avatar Aug 16 '19 06:08 tcNickolas

Is the magic command %kata specified in this repository(ie Quantum Katas) or some other repository?

Manvi-Agrawal avatar Aug 25 '20 16:08 Manvi-Agrawal

It's defined in Microsoft.Quantum.Katas package in this repo.

tcNickolas avatar Aug 25 '20 16:08 tcNickolas

Thanks @tcNickolas.

Manvi-Agrawal avatar Aug 25 '20 16:08 Manvi-Agrawal

@tcNickolas I would like to take up this issue.

Manvi-Agrawal avatar Aug 27 '20 04:08 Manvi-Agrawal

Sure, give it a try and let us know what design you come up with. It's a rather non-trivial issue, so we'll probably want to discuss it with @rmshaffer as well, who is familiar with IQ# internals.

Thank you!

tcNickolas avatar Aug 27 '20 17:08 tcNickolas

Hi @tcNickolas @rmshaffer, do you have any idea about where to see the Logger messages defined in KataMagic.cs?

Some examples are

Manvi-Agrawal avatar Oct 30 '20 08:10 Manvi-Agrawal

@Manvi-Agrawal Yes, you should set the environment variable IQSHARP_LOG_LEVEL=Debug (case-sensitive) and then restart the IQ# kernel. You should then see the log messages output to the console window where you launched Jupyter Notebook.

This logging is very noisy, but hopefully it is manageable.

rmshaffer avatar Oct 30 '20 12:10 rmshaffer

Hi @rmshaffer @tcNickolas, I am not able to see the modified error messages in the console window. For eg I tried to modify

I think I would need to create a new version of Microsoft.Quantum.Katas and update the project to see modified error messages. I looked up a few links:

Any help would be greatly appreciated.

Manvi-Agrawal avatar Feb 18 '21 07:02 Manvi-Agrawal

Yes, you need to update the Microsoft.Quantum.Katas project, build a NuGet package with a custom version out of it and use that package instead of the published NuGets.

Here's the sequence of steps that I use to build and use a private NuGet package:

  1. Build the Microsoft.Quantum.Katas project to produce a NuGet package (you can set version field in this file to get a version other than 1.0.0).
  2. Copy the generated .nupkg file to the folder in which your project resides (possibly PhaseEstimation kata).
  3. Set up NuGet.config file so that it allows the project to discover a NuGet package in the current folder in addition to the standard sources. Here's an example file (should be placed in project folder as well):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="Local Folder" value="." />
  </packageSources>
</configuration>
  1. Modify the .csproj file to change the version of the Microsoft.Quantum.Katas package to your version.

That should allow the project to pick up the custom version of the package, rather than the published one. I only tried this path with Q# projects, not with IQ#, so there might be some additional hiccups on the way.

Let me know if that path works!

tcNickolas avatar Feb 23 '21 05:02 tcNickolas

Hi @tcNickolas , seems that these steps aren't sufficient to see updated messages in IQ#. Also, a more general doubt: is there any smart way to debug kata and check_kata magic; or do we need to rely on log messages in IQ# console window?

Manvi-Agrawal avatar Feb 23 '21 07:02 Manvi-Agrawal

Hmm, then this might take a different path... I don't recall debugging these magics, but I'll try to set it up locally and ask around in the team to see how it's done.

Meanwhile, it sounded like you were going to work on #557 - I'd suggest doing that one first, since it only involves plain Q# :-)

tcNickolas avatar Feb 23 '21 09:02 tcNickolas

Thats a nice suggestion. I would be doing #557 first.

Manvi-Agrawal avatar Feb 23 '21 09:02 Manvi-Agrawal

Hi @tcNickolas , Now I am able to see updated log messages while running IQ# kernel in debug mode. I did the following additional steps:

  1. Set the environment variables IQSHARP_LOG_LEVEL='Debug' and IQSHARP_SKIP_AUTO_LOAD_PROJECT='True'
  2. Added <RootNameSpace>Microsoft.Quantum.Katas</RootNameSpace> to the <PropertyGroup> tag in Microsoft.Quantum.Katas.csproj
  3. Used %package magic to load the newly built nuget package Microsoft.Quantum.Katas and also load Microsoft.Quantum.Xunit
  4. Reloaded the workspace by using %workspace reload

I was thinking to update the contributing guide so that it would be easy for the new contributors to validate the changes in KataMagic and CheckKataMagic on their local machine. Would love to know your opinion on the same. Meanwhile I am thinking to prepare a design document since its not a trivial change...

Manvi-Agrawal avatar Mar 19 '21 16:03 Manvi-Agrawal

Hi @tcNickolas @rmshaffer, I think a good approach to solve this issue would be after the following issue is resolved https://github.com/microsoft/iqsharp/issues/277. Because currently IQ# recognises all Q# files in the root folder of jupyter notebook and compiles them into a DLL file like this

__ws__{assemblyVersion}{projectFilenamePart}__.dll

where assemblyVersion is the currently-executing IQ# assembly version and projectFilenamePart is the unique representation of the full path to the project file on disk.

This dll unfortunately does not contain the compiled C# code, so we can't call C# code currently. As a workaround, we could try to invoke dotnet build but then it has its own hassles as explained in https://github.com/microsoft/iqsharp/issues/277#issue-680415148

I would like to know your opinion on the above statement.

Manvi-Agrawal avatar Apr 01 '21 06:04 Manvi-Agrawal

A quick answer to the comment-before-last one: yes, documenting these steps in the contributor's guide would definitely be helpful!

Let me try and think of a workaround for this - I feel like I had an idea a couple of weeks ago which involved calling a Python test instead of C# one, but I can't promise that I'll recall it or that it will actually make sense :-)

tcNickolas avatar Apr 26 '21 20:04 tcNickolas

We will be migrating the Katas to the new QDK that will use a different infrastructure (see https://devblogs.microsoft.com/qsharp/introducing-the-azure-quantum-development-kit-preview/ for the announcement), so we won't continue to improve the existing infrastructure of Q# notebooks magics. Closing this issue as "won't fix".

tcNickolas avatar Sep 28 '23 23:09 tcNickolas