From b4b3ec97dd868a00633a5e72b8bdfe75635802b9 Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Thu, 19 Dec 2024 13:50:49 -0800 Subject: [PATCH] Update sample notebook (#2075) This is the sample notebook you can create from the command palette (which is very handy to avoid having to download/copy one to get started). It was a little out of date. This has some minor improvements. --- vscode/src/azure/workspaceActions.ts | 8 ++++-- vscode/src/notebookTemplate.ts | 37 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/vscode/src/azure/workspaceActions.ts b/vscode/src/azure/workspaceActions.ts index 9602e4faf5..964c8a3117 100644 --- a/vscode/src/azure/workspaceActions.ts +++ b/vscode/src/azure/workspaceActions.ts @@ -109,9 +109,13 @@ export function getPythonCodeForWorkspace( # should be configured and used for authentication. For more information, see # https://learn.microsoft.com/en-us/azure/developer/python/sdk/authentication-overview -import azure.quantum +from azure.quantum import Workspace -workspace = azure.quantum.Workspace( +# If using an access key, replace the below with: Workspace.from_connection_string(connection_string) +# Or set the "AZURE_QUANTUM_CONNECTION_STRING" environment variable and just use: Workspace() +# See https://learn.microsoft.com/en-us/azure/quantum/how-to-connect-workspace for more details. + +workspace = Workspace( subscription_id = "${subscriptionId || "MY_SUBSCRIPTION_ID"}", resource_group = "${resourceGroup || "MY_RESOURCE_GROUP"}", name = "${name || "MY_WORKSPACE_NAME"}", diff --git a/vscode/src/notebookTemplate.ts b/vscode/src/notebookTemplate.ts index bf3f6c45a3..59773cd373 100644 --- a/vscode/src/notebookTemplate.ts +++ b/vscode/src/notebookTemplate.ts @@ -99,9 +99,14 @@ import matplotlib.pyplot as plt import numpy as np from collections import Counter -results = qsharp.run("RandomBit()", shots=1000) +# Use save_events to suppress the automatic printing of events for every shot +results = qsharp.run("RandomBit()", shots=1000, save_events=True) + # Sort the results so that the histogram labels appear in the correct order +# We only care about the result values, not the event data, so extract just the result +results = [entry["result"] for entry in results] results.sort() + # Count the number of times each result appears counts = Counter(results) @@ -111,7 +116,35 @@ plt.title("RandomBit() Results") plt.bar(xlabels, counts) plt.xticks(xlabels, values) plt.show() - `, +`, + }, + { + kind: vscode.NotebookCellKind.Markup, + languageId: "markdown", + value: `## Q# widgets + +You can also use the \`qsharp_widgets\` package to visualize data. Install with \`pip install qsharp-widgets\``, + }, + { + kind: vscode.NotebookCellKind.Code, + languageId: "python", + value: `from qsharp_widgets import Histogram + +Histogram(results) +`, + }, + { + kind: vscode.NotebookCellKind.Code, + languageId: "python", + value: `from qsharp_widgets import EstimatesPanel + +estimate = qsharp.estimate("RandomBit()", [ + {"errorBudget": 0.333, "qubitParams": {"name": "qubit_gate_ns_e3"}}, + {"errorBudget": 0.333, "qubitParams": {"name": "qubit_gate_us_e4"}}, + {"errorBudget": 0.333, "qubitParams": {"name": "qubit_maj_ns_e6"}, "qecScheme": {"name": "floquet_code"}} +]) +EstimatesPanel(estimate) +`, }, { kind: vscode.NotebookCellKind.Markup,