From 6cdd16acf1de76f14f215b2c644594688788d6fc Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Tue, 22 Sep 2020 14:45:35 -0700 Subject: [PATCH 1/2] Added new EmptyArray function. (#379) --- src/Simulation/QsharpCore/Arrays/Empty.cs | 19 +++++++++++++++ src/Simulation/QsharpCore/Arrays/Empty.qs | 24 +++++++++++++++++++ .../IntrinsicTests/Arrays/Tests.qs | 22 +++++++++++++++++ .../IntrinsicTests/Random/Tests.qs | 3 +++ 4 files changed, 68 insertions(+) create mode 100644 src/Simulation/QsharpCore/Arrays/Empty.cs create mode 100644 src/Simulation/QsharpCore/Arrays/Empty.qs create mode 100644 src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Arrays/Tests.qs diff --git a/src/Simulation/QsharpCore/Arrays/Empty.cs b/src/Simulation/QsharpCore/Arrays/Empty.cs new file mode 100644 index 00000000000..7cb7448a393 --- /dev/null +++ b/src/Simulation/QsharpCore/Arrays/Empty.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System; +using Microsoft.Quantum.Simulation.Core; + +namespace Microsoft.Quantum.Arrays +{ + public partial class EmptyArray<__TElement__> + { + public class Native : EmptyArray<__TElement__> + { + public Native(IOperationFactory m) : base(m) { } + public override Func> __Body__ => _ => + new QArray<__TElement__>(); + } + } + +} diff --git a/src/Simulation/QsharpCore/Arrays/Empty.qs b/src/Simulation/QsharpCore/Arrays/Empty.qs new file mode 100644 index 00000000000..0d586def7a2 --- /dev/null +++ b/src/Simulation/QsharpCore/Arrays/Empty.qs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.Arrays { + + /// # Summary + /// Returns the empty array of a given type. + /// + /// # Type Parameters + /// ## 'TElement + /// The type of elements of the array. + /// + /// # Output + /// The empty array. + /// + /// # Example + /// ```Q# + /// let empty = EmptyArray(); + /// ``` + function EmptyArray<'TElement>() : 'TElement[] { + body intrinsic; + } + +} diff --git a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Arrays/Tests.qs b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Arrays/Tests.qs new file mode 100644 index 00000000000..3bbc428984a --- /dev/null +++ b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Arrays/Tests.qs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Quantum.Arrays { + open Microsoft.Quantum.Diagnostics; + + /// # Summary + /// Checks that empty arrays are indeed empty. + @Test("QuantumSimulator") + @Test("ToffoliSimulator") + function EmptyArraysAreEmpty() : Unit { + Fact( + Length(EmptyArray()) == 0, + "Empty array of type Int[] was not actually empty." + ); + Fact( + Length(EmptyArray<(Double, Pauli[])>()) == 0, + "Empty array of type (Double, Pauli[])[] was not actually empty." + ); + } + +} diff --git a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs index ab8416de9e6..aff82adbc8a 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs +++ b/src/Simulation/Simulators.Tests/TestProjects/IntrinsicTests/Random/Tests.qs @@ -1,3 +1,6 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + namespace Microsoft.Quantum.Tests { open Microsoft.Quantum.Intrinsic; open Microsoft.Quantum.Canon; From 330e4997923c883e179cb6209c4afcb9fe9ba28d Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Wed, 23 Sep 2020 15:35:33 -0700 Subject: [PATCH 2/2] Improving the logging of WorkspaceClientException to include more actionable information (#380) (#386) Improving the logging of WorkspaceClientException to include more informative data in its message from its inner exception. This is a follow up to #378 . - Instead of providing the full stack of the exception in the output, we extract more relevant fields from the source RestErrorException and provide them to the user. - Also, this change exposes header "x-ms-request-id"if present in the response, for additional debugging. Co-authored-by: Ricardo Espinoza --- .../Exceptions/WorkspaceClientException.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs b/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs index 821e3a64bc2..ee1b7293182 100644 --- a/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs +++ b/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.Azure.Quantum.Client.Models; using System; namespace Microsoft.Azure.Quantum.Exceptions @@ -67,9 +68,41 @@ public WorkspaceClientException( $"WorkspaceName: {workspaceName}{Environment.NewLine}" + $"BaseUri: {baseUri}{Environment.NewLine}" + $"JobId: {jobId}{Environment.NewLine}" + - (inner != null ? $"Inner Exception: {inner}" : string.Empty), + FormatInnerException(inner), inner) { } + + /// + /// Formats the contents of the inner exception in so it can be included in the + /// exception message and presented in an informative way. + /// + /// Inner exception that we want to include in the outer exception message. + /// + /// A string representing the contents of the inner exception. + /// + private static string FormatInnerException(Exception ex) + { + string formattedException = string.Empty; + if (ex != null) + { + formattedException += $"Server Error: {ex.Message}{Environment.NewLine}"; + + // Handle specific types of exceptions for additional data + if (ex is RestErrorException restErrorException) + { + formattedException += $"Error Code: {restErrorException?.Body?.Code}{Environment.NewLine}" + + $"Server message: {restErrorException?.Body?.Message}{Environment.NewLine}"; + + var headers = restErrorException?.Response?.Headers; + if (headers != null && headers.ContainsKey("x-ms-request-id")) + { + formattedException += $"Server Request Id: {headers["x-ms-request-id"]}{Environment.NewLine}"; + } + } + } + + return formattedException; + } } }