Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Conversation

@rmshaffer
Copy link
Contributor

Two changes to QuantumMachineFactory here, to address issues I found while consuming this from IQ#:

  • Take an IWorkspace parameter instead of a Workspace parameter.
  • Load the type directly from the already-loaded assembly with the expected name, rather than using Type.GetType(), which seems to sometimes require an AssemblyQualifiedName including version number and public key token in order to load the type successfully. (Open to suggestions here, in case there's a simpler way of doing this, but this seems to work.)

@rmshaffer rmshaffer requested a review from bamarsha May 18, 2020 21:42
Copy link
Contributor

@bamarsha bamarsha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to create an IonQ machine, I get this:

Unhandled exception: System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.ThrowHelper.ThrowNoMatchException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.Azure.Quantum.QuantumMachineFactory.CreateMachine(IWorkspace workspace, String targetName, String storageAccountConnectionString) in C:\Users\samarsha\Source\Repos\microsoft\qsharp-runtime\src\Azure\Azure.Quantum.Client\Machine\QuantumMachineFactory.cs:line 30
   at Microsoft.Quantum.CsharpGeneration.EntryPointDriver.Azure.CreateMachine(AzureSettings settings) in C:\Users\samarsha\Source\Repos\microsoft\qsharp-runtime\src\Simulation\EntryPointDriver\Azure.cs:line 60
   at Microsoft.Quantum.CsharpGeneration.EntryPointDriver.Azure.Submit[TIn,TOut](IEntryPoint`2 entryPoint, ParseResult parseResult, AzureSettings settings) in C:\Users\samarsha\Source\Repos\microsoft\qsharp-runtime\src\Simulation\EntryPointDriver\Azure.cs:line 28
   at Microsoft.Quantum.CsharpGeneration.EntryPointDriver.Driver`3.Submit(ParseResult parseResult, AzureSettings settings) in C:\Users\samarsha\Source\Repos\microsoft\qsharp-runtime\src\Simulation\EntryPointDriver\Driver.cs:line 124
   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()

My understanding of AppDomain.CurrentDomain.GetAssemblies() is that it only returns assemblies that have been loaded into the currently running program (e.g. a type in the assembly has been used at least once before GetAssemblies() is called). Since the IonQ assembly hasn't been used before the standalone executable needs to instantiate it, it looks like CreateMachine isn't able to find it for me.

@rmshaffer
Copy link
Contributor Author

Thanks for trying this out! Did you do anything special such that the existing Type.GetType() call was succeeding? When I tried to call the code using Type.GetType(), it couldn't find the assembly (even if it had already been loaded) unless I also included the version and the PublicKeyToken in the type string, i.e., in a format like this:
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
I suppose we could always expose both versions of this, if we need to obtain the Type in different ways. IQ# does its own package loading and management at runtime, so I guess it's not surprising that this behaves differently.


In reply to: 414007852 [](ancestors = 414007852)

@bamarsha
Copy link
Contributor

No, I don't think I needed to do anything to get Type.GetType to work. Could you post the error message you get from it?

@bamarsha
Copy link
Contributor

Could it have something to do with IQ# using signed versions of the IonQ assembly? That could mean that Type.GetType would fail for the standalone executable as well if it uses a signed version. (I'm not sure if the versions we have been using for testing have been signed or not.) We could try specifying just the assembly name and public key token (but not the version), but I am not sure if the public key token stays the same or changes when the version number is changed.

@rmshaffer
Copy link
Contributor Author

Below is the error I get. I'll try just specifying the PublicKeyToken and not the version; I think that should be consistent for all signed versions, but I'd need to confirm this.

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'Microsoft.Quantum.Providers.IonQ, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
  Source=<Cannot evaluate the exception source>
  StackTrace:
  System.Private.CoreLib.dll!System.RuntimeTypeHandle.GetTypeByName(string name, bool throwOnError, bool ignoreCase, System.Runtime.CompilerServices.StackCrawlMarkHandle stackMark, System.Runtime.CompilerServices.ObjectHandleOnStack assemblyLoadContext, bool loadTypeFromPartialName, System.Runtime.CompilerServices.ObjectHandleOnStack type, System.Runtime.CompilerServices.ObjectHandleOnStack keepalive) Unknown
  System.Private.CoreLib.dll!System.RuntimeTypeHandle.GetTypeByName(string name, bool throwOnError, bool ignoreCase, ref System.Threading.StackCrawlMark stackMark, System.Runtime.Loader.AssemblyLoadContext assemblyLoadContext, bool loadTypeFromPartialName) Unknown
  System.Private.CoreLib.dll!System.RuntimeType.GetType(string typeName, bool throwOnError, bool ignoreCase, ref System.Threading.StackCrawlMark stackMark) Unknown
  System.Private.CoreLib.dll!System.Type.GetType(string typeName, bool throwOnError) Unknown
  Microsoft.Azure.Quantum.Client.dll!Microsoft.Azure.Quantum.QuantumMachineFactory.CreateMachine(Microsoft.Azure.Quantum.Workspace workspace, string targetName, string storageAccountConnectionString) Unknown
  Microsoft.Quantum.IQSharp.AzureClient.dll!Microsoft.Quantum.IQSharp.AzureClient.AzureClient.SubmitJobAsync(Microsoft.Jupyter.Core.IChannel channel, Microsoft.Quantum.IQSharp.IOperationResolver operationResolver, string operationName) Line 162 C#
> Microsoft.Quantum.IQSharp.AzureClient.dll!Microsoft.Quantum.IQSharp.AzureClient.SubmitMagic.RunAsync(string input, Microsoft.Jupyter.Core.IChannel channel) Line 63 C#
  Microsoft.Quantum.IQSharp.AzureClient.dll!Microsoft.Quantum.IQSharp.AzureClient.AzureClientMagicBase.Run(string input, Microsoft.Jupyter.Core.IChannel channel) Line 35 C#


In reply to: 630504039 [](ancestors = 630504039)

@rmshaffer rmshaffer requested a review from bamarsha May 19, 2020 19:47
@rmshaffer rmshaffer merged commit 1b70dc2 into feature/azure-quantum-preview May 20, 2020
@rmshaffer rmshaffer deleted the rmshaffer/quantum-machine-reflection branch May 20, 2020 00:22
bamarsha added a commit that referenced this pull request May 28, 2020
)

* Create new project for entry point driver

* Fix entry point option values

* Add missing dashes to simulator options

* Update root namespace

* Add driver to nuget package

* Manually add EntryPointDriver's dependencies to package

* Add doc comments to Validation

* Rewrite FindNuspecReferences.ps1

* Type parameterize IEntryPoint

* Add reference to EntryPointDriver from tests

* Remove entry point driver resources

* Rename EntryPointDriver assembly

* Remove unneeded generated code

* Add documentation to IEntryPoint

* Update Driver documentation

* Enable C# nullable feature

* Avoid multiple enumeration

* Remove unnecessary identifier quotations

* Remove "Get" prefix

* Update variable name/comment

* Make Validation and value parsers private/internal

* Support IQArray types directly

* Remove single-class namespace

* Use type parameter in CreateOption

* Combine some excessively separated functions

* Use WithSuggestions instead of AddSuggestions

* Add entry point argument tuple to interface

* Fix nested tuples in arguments

* Add entry point info to IEntryPoint

* Add submit command

* Fix typo

Co-authored-by: bettinaheim <34236215+bettinaheim@users.noreply.github.com>

* Use only one line for shadowing warning

* Removed type parameters from IQuantumMachine interface. (#185)

* Removed type parameters from IQuantumMachine interface.

* Addressed Sarah's feedback.

* Update src/Simulation/Core/IQuantumMachineOutput.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Use the new IQuantumMachine interface

* Add TODO for histogram output

* Add command-line options for Azure submission

* Small code style fixes

* Use real provider for Azure submission

* Remove TODO from the wrong place

* Use SimulatorMachine based on target name

* Add option for number of shots

* Validate that shots is positive

* Use DefaultIfShadowed for Azure settings

* Simplify adding/defaulting options

* Make SimulatorOption non-static

* Add AddOptionsIfAvailable method

* Fix option aliases missing dashes

* Disable command if required options are shadowed

* Add copyright headers

* Add histogram option

* Make histogram pretty

* Fix shadowing

* Update histogram divider dash

* Preserve required state of options

* Simulator option is not required

* Use SubmitAsync instead of ExecuteAsync

* Add access token option

* Rename ToWorkspace to CreateWorkspace

* Remove "Azure" from option descriptions

* Print job ID instead of URI for now

* Add --base-uri option

* Rename --access to --aad-token

* Rename --id-only to --output

* Add OptionInfo class

* Update --shots validator message

* Update --base-uri description

* Add TODO for "submit" tests

* Replace NotImplementedExceptions with NotSupportedExceptions

* More specific TODO for number of shots

* Don't include default value for required options

* Hide "submit" command

* Update help test

* Add manifest with qsharp-runtime assemblies requiring signing (#200) (#202)

* Add manifest with assemblies requiring signing

* Add manifest.ps1 to build steps

* Run manifest after build completes

* Fix manifest.ps1 script error

* Fix to manifest.ps1

* Revert unnecessary commit 466dc8c

* Update TODO comments

* Add test for submit help text

* Add more tests for submit command

* Add Microsoft.Azure.Quantum.Client sources and QuantumMachineFactory (#203)

* Add Microsoft.Azure.Quantum.Client sources

* Add project reference to Microsoft.Quantum.Runtime.Core

* Add reference to Microsoft.Azure.Quantum.Client

* Re-add missing summary start tag

* Add Honeywell to QuantumMachineFactory

* Allow reflection to find type in loaded assembly (#205)

* Update reflection to look for signed assembly first

* Use IWorkspace instead of Workspace

* Catch exceptions from GetType call

* Use package references to runtime components in EntryPointDriver

* Revert package references to project references

* Add Microsoft.Azure.Quantum.Client as NuGet dependency

* Add stubs for new methods in NothingMachine

* Add --dry-run option

* Set shots in submission context

* Add blank line before validation message

* Created AzzureQuantumException. (#216)

This change adds AzureQuantumException which is the base exception to throw or create new exceptions related to the Azure quantum service or storage.

* Merge latest master to have access to QuantumProcessorTranslationException (#217)

* Created QuantumProcessorException (#212)

* Created QuantumProcessorException.

* Provided a default message but added the option to supply a custom one.

* Renamed to QuantumProcessorTranslationException.

* Improved the default message.

* Updating placeholder names to provider names (#215)

Co-authored-by: Stefan J. Wernli <swernli@microsoft.com>

* Show warning message for OutputFormat.FriendlyUri

* Fix tests

* Rename test project

* Add test for unknown target

* Add test project to test.ps1

* Handle Azure quantum known exceptions (#218)

* Add the three standard constructors for the newly created exceptions.

* Created QuantumMachineClientException and incorporated it (and other exceptions) into the standalone logic.

* Removed exception that is not needed.

* Improved error message.

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update copyright headers in Azure Quantum client

* Remove TODO for audience URI

Co-authored-by: bettinaheim <34236215+bettinaheim@users.noreply.github.com>
Co-authored-by: César Zaragoza Cortés <cesar.zaragoza@outlook.com>
Co-authored-by: Ryan Shaffer <ryan.shaffer@microsoft.com>
Co-authored-by: Stefan J. Wernli <swernli@microsoft.com>
bamarsha pushed a commit that referenced this pull request May 28, 2020
* Update reflection to look for signed assembly first

* Use IWorkspace instead of Workspace

* Catch exceptions from GetType call
AniketDalvi pushed a commit that referenced this pull request Jul 1, 2020
)

* Create new project for entry point driver

* Fix entry point option values

* Add missing dashes to simulator options

* Update root namespace

* Add driver to nuget package

* Manually add EntryPointDriver's dependencies to package

* Add doc comments to Validation

* Rewrite FindNuspecReferences.ps1

* Type parameterize IEntryPoint

* Add reference to EntryPointDriver from tests

* Remove entry point driver resources

* Rename EntryPointDriver assembly

* Remove unneeded generated code

* Add documentation to IEntryPoint

* Update Driver documentation

* Enable C# nullable feature

* Avoid multiple enumeration

* Remove unnecessary identifier quotations

* Remove "Get" prefix

* Update variable name/comment

* Make Validation and value parsers private/internal

* Support IQArray types directly

* Remove single-class namespace

* Use type parameter in CreateOption

* Combine some excessively separated functions

* Use WithSuggestions instead of AddSuggestions

* Add entry point argument tuple to interface

* Fix nested tuples in arguments

* Add entry point info to IEntryPoint

* Add submit command

* Fix typo

Co-authored-by: bettinaheim <34236215+bettinaheim@users.noreply.github.com>

* Use only one line for shadowing warning

* Removed type parameters from IQuantumMachine interface. (#185)

* Removed type parameters from IQuantumMachine interface.

* Addressed Sarah's feedback.

* Update src/Simulation/Core/IQuantumMachineOutput.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Use the new IQuantumMachine interface

* Add TODO for histogram output

* Add command-line options for Azure submission

* Small code style fixes

* Use real provider for Azure submission

* Remove TODO from the wrong place

* Use SimulatorMachine based on target name

* Add option for number of shots

* Validate that shots is positive

* Use DefaultIfShadowed for Azure settings

* Simplify adding/defaulting options

* Make SimulatorOption non-static

* Add AddOptionsIfAvailable method

* Fix option aliases missing dashes

* Disable command if required options are shadowed

* Add copyright headers

* Add histogram option

* Make histogram pretty

* Fix shadowing

* Update histogram divider dash

* Preserve required state of options

* Simulator option is not required

* Use SubmitAsync instead of ExecuteAsync

* Add access token option

* Rename ToWorkspace to CreateWorkspace

* Remove "Azure" from option descriptions

* Print job ID instead of URI for now

* Add --base-uri option

* Rename --access to --aad-token

* Rename --id-only to --output

* Add OptionInfo class

* Update --shots validator message

* Update --base-uri description

* Add TODO for "submit" tests

* Replace NotImplementedExceptions with NotSupportedExceptions

* More specific TODO for number of shots

* Don't include default value for required options

* Hide "submit" command

* Update help test

* Add manifest with qsharp-runtime assemblies requiring signing (#200) (#202)

* Add manifest with assemblies requiring signing

* Add manifest.ps1 to build steps

* Run manifest after build completes

* Fix manifest.ps1 script error

* Fix to manifest.ps1

* Revert unnecessary commit 466dc8c

* Update TODO comments

* Add test for submit help text

* Add more tests for submit command

* Add Microsoft.Azure.Quantum.Client sources and QuantumMachineFactory (#203)

* Add Microsoft.Azure.Quantum.Client sources

* Add project reference to Microsoft.Quantum.Runtime.Core

* Add reference to Microsoft.Azure.Quantum.Client

* Re-add missing summary start tag

* Add Honeywell to QuantumMachineFactory

* Allow reflection to find type in loaded assembly (#205)

* Update reflection to look for signed assembly first

* Use IWorkspace instead of Workspace

* Catch exceptions from GetType call

* Use package references to runtime components in EntryPointDriver

* Revert package references to project references

* Add Microsoft.Azure.Quantum.Client as NuGet dependency

* Add stubs for new methods in NothingMachine

* Add --dry-run option

* Set shots in submission context

* Add blank line before validation message

* Created AzzureQuantumException. (#216)

This change adds AzureQuantumException which is the base exception to throw or create new exceptions related to the Azure quantum service or storage.

* Merge latest master to have access to QuantumProcessorTranslationException (#217)

* Created QuantumProcessorException (#212)

* Created QuantumProcessorException.

* Provided a default message but added the option to supply a custom one.

* Renamed to QuantumProcessorTranslationException.

* Improved the default message.

* Updating placeholder names to provider names (#215)

Co-authored-by: Stefan J. Wernli <swernli@microsoft.com>

* Show warning message for OutputFormat.FriendlyUri

* Fix tests

* Rename test project

* Add test for unknown target

* Add test project to test.ps1

* Handle Azure quantum known exceptions (#218)

* Add the three standard constructors for the newly created exceptions.

* Created QuantumMachineClientException and incorporated it (and other exceptions) into the standalone logic.

* Removed exception that is not needed.

* Improved error message.

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update src/Simulation/EntryPointDriver/Azure.cs

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

Co-authored-by: Sarah Marshall <33814365+samarsha@users.noreply.github.com>

* Update copyright headers in Azure Quantum client

* Remove TODO for audience URI

Co-authored-by: bettinaheim <34236215+bettinaheim@users.noreply.github.com>
Co-authored-by: César Zaragoza Cortés <cesar.zaragoza@outlook.com>
Co-authored-by: Ryan Shaffer <ryan.shaffer@microsoft.com>
Co-authored-by: Stefan J. Wernli <swernli@microsoft.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants