From 874821f650e8ad1fb5686593818c58fffaab77c1 Mon Sep 17 00:00:00 2001 From: Ryan Shaffer Date: Mon, 18 May 2020 17:36:42 -0400 Subject: [PATCH 1/5] Allow reflection to find type in loaded assembly --- .../Machine/QuantumMachineFactory.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs index d6ca6671156..aae55ed8711 100644 --- a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs +++ b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs @@ -1,8 +1,10 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#nullable enable + using System; -using System.Threading.Tasks; +using System.Linq; using Microsoft.Quantum.Runtime; namespace Microsoft.Azure.Quantum @@ -16,7 +18,7 @@ public static class QuantumMachineFactory /// The execution target for job submission. /// The connection string for the Azure storage account. /// A quantum machine for job submission targeting targetName. - public static IQuantumMachine? CreateMachine(Workspace workspace, string targetName, string storageAccountConnectionString) + public static IQuantumMachine? CreateMachine(IWorkspace workspace, string targetName, string storageAccountConnectionString) { if (string.IsNullOrEmpty(targetName)) { @@ -25,13 +27,13 @@ public static class QuantumMachineFactory if (targetName.StartsWith("ionq.")) { - var ionQType = Type.GetType( - "Microsoft.Quantum.Providers.IonQ.Targets.IonQQuantumMachine, Microsoft.Quantum.Providers.IonQ", - throwOnError: true); + var ionQType = AppDomain.CurrentDomain.GetAssemblies() + .First(a => a.FullName.StartsWith("Microsoft.Quantum.Providers.IonQ,")) + .GetType("Microsoft.Quantum.Providers.IonQ.Targets.IonQQuantumMachine", throwOnError: true); return (IQuantumMachine)Activator.CreateInstance( ionQType, targetName, storageAccountConnectionString, workspace); } - + return null; } } From 92a6fd993456fdac8efe221113b8b0f7df204606 Mon Sep 17 00:00:00 2001 From: Ryan Shaffer Date: Tue, 19 May 2020 15:35:43 -0400 Subject: [PATCH 2/5] Revert "Allow reflection to find type in loaded assembly" This reverts commit 874821f650e8ad1fb5686593818c58fffaab77c1. --- .../Machine/QuantumMachineFactory.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs index aae55ed8711..d6ca6671156 100644 --- a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs +++ b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs @@ -1,10 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -#nullable enable - using System; -using System.Linq; +using System.Threading.Tasks; using Microsoft.Quantum.Runtime; namespace Microsoft.Azure.Quantum @@ -18,7 +16,7 @@ public static class QuantumMachineFactory /// The execution target for job submission. /// The connection string for the Azure storage account. /// A quantum machine for job submission targeting targetName. - public static IQuantumMachine? CreateMachine(IWorkspace workspace, string targetName, string storageAccountConnectionString) + public static IQuantumMachine? CreateMachine(Workspace workspace, string targetName, string storageAccountConnectionString) { if (string.IsNullOrEmpty(targetName)) { @@ -27,13 +25,13 @@ public static class QuantumMachineFactory if (targetName.StartsWith("ionq.")) { - var ionQType = AppDomain.CurrentDomain.GetAssemblies() - .First(a => a.FullName.StartsWith("Microsoft.Quantum.Providers.IonQ,")) - .GetType("Microsoft.Quantum.Providers.IonQ.Targets.IonQQuantumMachine", throwOnError: true); + var ionQType = Type.GetType( + "Microsoft.Quantum.Providers.IonQ.Targets.IonQQuantumMachine, Microsoft.Quantum.Providers.IonQ", + throwOnError: true); return (IQuantumMachine)Activator.CreateInstance( ionQType, targetName, storageAccountConnectionString, workspace); } - + return null; } } From 7c299d4a853a22cac2055ae41d48c4fd0724236a Mon Sep 17 00:00:00 2001 From: Ryan Shaffer Date: Tue, 19 May 2020 15:45:40 -0400 Subject: [PATCH 3/5] Update reflection to look for signed assembly first --- .../Machine/QuantumMachineFactory.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs index ef9654c10ca..97df6753a3c 100644 --- a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs +++ b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs @@ -17,21 +17,26 @@ public static class QuantumMachineFactory /// A quantum machine for job submission targeting targetName. public static IQuantumMachine? CreateMachine(Workspace workspace, string targetName, string storageAccountConnectionString) { - if (string.IsNullOrEmpty(targetName)) - { - return null; - } - var machineName = - targetName.StartsWith("ionq.") + targetName is null + ? null + : targetName.StartsWith("ionq.") ? "Microsoft.Quantum.Providers.IonQ.Targets.IonQQuantumMachine, Microsoft.Quantum.Providers.IonQ" : targetName.StartsWith("honeywell.") ? "Microsoft.Quantum.Providers.Honeywell.Targets.HoneywellQuantumMachine, Microsoft.Quantum.Providers.Honeywell" : null; - return machineName is null + + // First try to load the signed assembly with the correct version, then try the unsigned one. + var machineType = + machineName is null + ? null + : Type.GetType($"{machineName}, Version={typeof(Workspace).Assembly.GetName().Version}, Culture=neutral, PublicKeyToken=40866b40fd95c7f5") + ?? Type.GetType(machineName, throwOnError: true); + + return machineType is null ? null : (IQuantumMachine)Activator.CreateInstance( - Type.GetType(machineName, throwOnError: true), + machineType, targetName, storageAccountConnectionString, workspace); From bb70c1c580f13a85ad38848f350db0a05f3faed6 Mon Sep 17 00:00:00 2001 From: Ryan Shaffer Date: Tue, 19 May 2020 15:50:25 -0400 Subject: [PATCH 4/5] Use IWorkspace instead of Workspace --- .../Azure.Quantum.Client/Machine/QuantumMachineFactory.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs index 97df6753a3c..9241d13b7eb 100644 --- a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs +++ b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs @@ -15,7 +15,7 @@ public static class QuantumMachineFactory /// The execution target for job submission. /// The connection string for the Azure storage account. /// A quantum machine for job submission targeting targetName. - public static IQuantumMachine? CreateMachine(Workspace workspace, string targetName, string storageAccountConnectionString) + public static IQuantumMachine? CreateMachine(IWorkspace workspace, string targetName, string storageAccountConnectionString) { var machineName = targetName is null @@ -30,7 +30,7 @@ targetName is null var machineType = machineName is null ? null - : Type.GetType($"{machineName}, Version={typeof(Workspace).Assembly.GetName().Version}, Culture=neutral, PublicKeyToken=40866b40fd95c7f5") + : Type.GetType($"{machineName}, Version={typeof(IWorkspace).Assembly.GetName().Version}, Culture=neutral, PublicKeyToken=40866b40fd95c7f5") ?? Type.GetType(machineName, throwOnError: true); return machineType is null From d9a58eb245f57e77415fa5ae0a96c7581c611172 Mon Sep 17 00:00:00 2001 From: Ryan Shaffer Date: Tue, 19 May 2020 17:02:32 -0400 Subject: [PATCH 5/5] Catch exceptions from GetType call --- .../Machine/QuantumMachineFactory.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs index 9241d13b7eb..94ab65f2368 100644 --- a/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs +++ b/src/Azure/Azure.Quantum.Client/Machine/QuantumMachineFactory.cs @@ -26,12 +26,21 @@ targetName is null ? "Microsoft.Quantum.Providers.Honeywell.Targets.HoneywellQuantumMachine, Microsoft.Quantum.Providers.Honeywell" : null; - // First try to load the signed assembly with the correct version, then try the unsigned one. - var machineType = - machineName is null - ? null - : Type.GetType($"{machineName}, Version={typeof(IWorkspace).Assembly.GetName().Version}, Culture=neutral, PublicKeyToken=40866b40fd95c7f5") - ?? Type.GetType(machineName, throwOnError: true); + Type machineType = null; + if (machineName != null) + { + // First try to load the signed assembly with the correct version, then try the unsigned one. + try + { + machineType = Type.GetType($"{machineName}, Version={typeof(IWorkspace).Assembly.GetName().Version}, Culture=neutral, PublicKeyToken=40866b40fd95c7f5"); + } + catch + { + machineType = null; + } + + machineType ??= Type.GetType(machineName, throwOnError: true); + } return machineType is null ? null