Skip to content

Commit

Permalink
Update sample notebook (#2075)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
billti authored Dec 19, 2024
1 parent 1760a9c commit b4b3ec9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 6 additions & 2 deletions vscode/src/azure/workspaceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}",
Expand Down
37 changes: 35 additions & 2 deletions vscode/src/notebookTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit b4b3ec9

Please sign in to comment.