Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
Obtained by running
```
ruff format
```
  • Loading branch information
thierry-martinez committed Dec 6, 2024
1 parent 576fc5e commit 253e6a2
Showing 1 changed file with 70 additions and 83 deletions.
153 changes: 70 additions & 83 deletions VBQC_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
"## This circuit, with |0> states as inputs,\n",
"# yields deterministic outcomes (BQP error p =0)\n",
"circuit = Circuit(2)\n",
"circuit.cnot(0,1)\n",
"circuit.cnot(0, 1)\n",
"circuit.h(0)\n",
"circuit.h(1)\n",
"pattern = circuit.transpile().pattern ## 6 nodes\n",
"pattern = circuit.transpile().pattern ## 6 nodes\n",
"pattern.standardize()\n",
"pattern.print_pattern()\n",
"\n",
Expand All @@ -83,11 +83,10 @@
"\n",
"for _i in range(1000):\n",
" pattern.simulate_pattern(backend=\"statevector\", input_state=states)\n",
" if pattern.results[4]==0 and pattern.results[5]==0:\n",
" if pattern.results[4] == 0 and pattern.results[5] == 0:\n",
" n_good_output += 1\n",
"\n",
"print(f\"Number of occurrences of output '00' : {n_good_output}\")\n",
"\n"
"print(f\"Number of occurrences of output '00' : {n_good_output}\")"
]
},
{
Expand All @@ -105,17 +104,14 @@
}
],
"source": [
"\n",
"\n",
"secrets = Secrets(r=True, a=True, theta=True)\n",
"\n",
"client = Client(pattern=pattern, secrets=secrets, input_state=states)\n",
"\n",
"test_runs = client.create_test_runs()\n",
"\n",
"\n",
"number_of_traps = sum([len(run.trap_qubits)\n",
" for run in test_runs])\n",
"number_of_traps = sum([len(run.trap_qubits) for run in test_runs])\n",
"n_nodes = len(client.graph[0])\n",
"print(f\"The graph of the pattern has {n_nodes} nodes.\")\n",
"print(f\"There are {number_of_traps} traps in total. (VBQC uses single-qubit traps)\")"
Expand Down Expand Up @@ -173,8 +169,8 @@
"from matplotlib.colors import LogNorm\n",
"\n",
"# Trappified scheme parameters\n",
"d = 50 # nr of computation rounds\n",
"t = 50 # nr of test rounds\n",
"d = 50 # nr of computation rounds\n",
"t = 50 # nr of test rounds\n",
"N = d + t\n",
"rounds = list(range(N))\n",
"rd.shuffle(rounds)\n",
Expand All @@ -191,11 +187,8 @@
"\n",
"print(f\"Iterating {N} times with {d} computation rounds and {t} trap rounds.\")\n",
"for p in p_values:\n",
"\n",
" # Defining the noise model (depolarizing noise)\n",
" noise=VBQCNoiseModel(\n",
" entanglement_error_prob=p\n",
" )\n",
" noise = VBQCNoiseModel(entanglement_error_prob=p)\n",
" print(f\"Attempt with noise model p={p}\")\n",
"\n",
" # Recording outcomes/traps failures\n",
Expand Down Expand Up @@ -230,12 +223,12 @@
"\n",
" # Combine results\n",
" all_histograms[p] = outcomes_histogram\n",
" if t!= 0:\n",
" failed_traps_histograms[p]=n_failed_traps/(t)\n",
" if t != 0:\n",
" failed_traps_histograms[p] = n_failed_traps / (t)\n",
"\n",
"# Combine all unique outcomes from all histograms\n",
"all_outcomes = sorted(set().union(*[hist.keys() for hist in all_histograms.values()]))\n",
"print(\"Proportion of failed traps in for different noise levels: \", failed_traps_histograms)\n"
"print(\"Proportion of failed traps in for different noise levels: \", failed_traps_histograms)"
]
},
{
Expand Down Expand Up @@ -1381,11 +1374,12 @@
"\n",
"\n",
"# Function to get color based on p value\n",
"def get_heatmap_color(p, colormap_name='coolwarm', vmin=0.1, vmax=1):\n",
"def get_heatmap_color(p, colormap_name=\"coolwarm\", vmin=0.1, vmax=1):\n",
" colormap = cm.get_cmap(colormap_name)\n",
" norm = LogNorm(vmin=vmin, vmax=vmax) # Normalize p within [vmin, vmax]\n",
" return to_hex(colormap(norm(p))) # Convert to hex color for Plotly\n",
"\n",
"\n",
"# Initialize the figure\n",
"fig = go.Figure()\n",
"\n",
Expand All @@ -1403,44 +1397,39 @@
" go.Bar(\n",
" x=all_outcomes,\n",
" y=frequencies,\n",
" name=f'p={p}', # Legend label\n",
" name=f\"p={p}\", # Legend label\n",
" marker=dict(color=colors), # Apply custom colors\n",
" visible=(p == 0) # Only p=0 is visible by default\n",
" visible=(p == 0), # Only p=0 is visible by default\n",
" )\n",
" )\n",
"\n",
"fig.update_layout(\n",
" title=\"Output bitstring occurrences, per noise level p\",\n",
" updatemenus=[\n",
" {\n",
" 'type': 'buttons',\n",
" 'direction': 'right',\n",
" 'x': 0.5, # Horizontal position of the buttons\n",
" 'y': 1.15, # Vertical position above the chart\n",
" 'xanchor': 'center', # Anchor buttons horizontally center\n",
" 'buttons': [\n",
" \"type\": \"buttons\",\n",
" \"direction\": \"right\",\n",
" \"x\": 0.5, # Horizontal position of the buttons\n",
" \"y\": 1.15, # Vertical position above the chart\n",
" \"xanchor\": \"center\", # Anchor buttons horizontally center\n",
" \"buttons\": [\n",
" {\n",
" 'label': f'p= {key}',\n",
" 'method': 'update',\n",
" 'args': [{'visible': [i == idx for i in range(len(all_histograms))]}]\n",
" \"label\": f\"p= {key}\",\n",
" \"method\": \"update\",\n",
" \"args\": [{\"visible\": [i == idx for i in range(len(all_histograms))]}],\n",
" }\n",
" for idx, key in enumerate(all_histograms)\n",
" ] + [\n",
" {\n",
" 'label': 'Show All',\n",
" 'method': 'update',\n",
" 'args': [{'visible': [True] * len(all_histograms)}]\n",
" }\n",
" ]\n",
" + [{\"label\": \"Show All\", \"method\": \"update\", \"args\": [{\"visible\": [True] * len(all_histograms)}]}],\n",
" }\n",
" ]\n",
" ],\n",
")\n",
"\n",
"# Set initial visibility (show only the first chart by default)\n",
"initial_visibility = [True] + [False] * (len(all_histograms) - 1)\n",
"fig.update_traces(visible=initial_visibility[0])\n",
"\n",
"fig.show()\n"
"fig.show()"
]
},
{
Expand Down Expand Up @@ -2850,24 +2839,26 @@
"\n",
"\n",
"# Function to get color based on p value\n",
"def get_heatmap_color(p, colormap_name='coolwarm', vmin=0.1, vmax=1):\n",
"def get_heatmap_color(p, colormap_name=\"coolwarm\", vmin=0.1, vmax=1):\n",
" colormap = cm.get_cmap(colormap_name)\n",
" norm = LogNorm(vmin=vmin, vmax=vmax) # Normalize p within [vmin, vmax]\n",
" return to_hex(colormap(norm(p))) # Convert to hex color for Plotly\n",
"\n",
"\n",
"# Initialize the figure with subplots\n",
"fig = make_subplots(\n",
" rows=1, cols=2,\n",
" rows=1,\n",
" cols=2,\n",
" column_widths=[0.7, 0.3],\n",
" subplot_titles=(\"Output Bitstring Occurrences\", \"Failed/Passed Traps\"),\n",
" specs=[[{\"type\": \"bar\"}, {\"type\": \"pie\"}]]\n",
" specs=[[{\"type\": \"bar\"}, {\"type\": \"pie\"}]],\n",
")\n",
"\n",
"# Store the p values in a list to access them by index in buttons\n",
"p_values = list(all_histograms.keys())\n",
"\n",
"# Add bar charts and pie charts for different 'p' values\n",
"colors = ['#EF553B', '#a5ff33'] # Red for 'Failed traps', Green for 'Passed traps'\n",
"colors = [\"#EF553B\", \"#a5ff33\"] # Red for 'Failed traps', Green for 'Passed traps'\n",
"\n",
"for _idx, (p, histogram) in enumerate(all_histograms.items()):\n",
" frequencies = [histogram.get(outcome, 0) for outcome in all_outcomes]\n",
Expand All @@ -2880,55 +2871,52 @@
" go.Bar(\n",
" x=all_outcomes,\n",
" y=frequencies,\n",
" name=f'p={p}', # Legend label\n",
" name=f\"p={p}\", # Legend label\n",
" marker=dict(color=bar_colors), # Apply custom colors\n",
" visible=(p == p_values[0]) # Only the first p is visible by default\n",
" visible=(p == p_values[0]), # Only the first p is visible by default\n",
" ),\n",
" row=1, col=1\n",
" row=1,\n",
" col=1,\n",
" )\n",
"\n",
" # Pie chart\n",
" failed_value = failed_traps_histograms.get(p, 0) # Fetch failed traps for current p\n",
" fig.add_trace(\n",
" go.Pie(\n",
" labels=['Failed traps', 'Passed traps'],\n",
" labels=[\"Failed traps\", \"Passed traps\"],\n",
" values=[failed_value, 1 - failed_value],\n",
" marker=dict(colors=colors),\n",
" name=f'p={p}',\n",
" visible=(p == p_values[0])\n",
" name=f\"p={p}\",\n",
" visible=(p == p_values[0]),\n",
" ),\n",
" row=1, col=2\n",
" row=1,\n",
" col=2,\n",
" )\n",
"\n",
"# Update layout with buttons\n",
"fig.update_layout(\n",
" title=\"Output bitstring occurrences and trap results per noise level p\",\n",
" updatemenus=[\n",
" {\n",
" 'type': 'buttons',\n",
" 'direction': 'right',\n",
" 'x': 0.5,\n",
" 'y': 1.15,\n",
" 'xanchor': 'center',\n",
" 'buttons': [\n",
" \"type\": \"buttons\",\n",
" \"direction\": \"right\",\n",
" \"x\": 0.5,\n",
" \"y\": 1.15,\n",
" \"xanchor\": \"center\",\n",
" \"buttons\": [\n",
" {\n",
" 'label': f'p= {key}',\n",
" 'method': 'update',\n",
" 'args': [{'visible': [(i // 2 == idx) for i in range(2 * len(all_histograms))]}]\n",
" \"label\": f\"p= {key}\",\n",
" \"method\": \"update\",\n",
" \"args\": [{\"visible\": [(i // 2 == idx) for i in range(2 * len(all_histograms))]}],\n",
" }\n",
" for idx, key in enumerate(all_histograms)\n",
" ] + [\n",
" {\n",
" 'label': 'Show All',\n",
" 'method': 'update',\n",
" 'args': [{'visible': [True] * (2 * len(all_histograms))}]\n",
" }\n",
" ]\n",
" + [{\"label\": \"Show All\", \"method\": \"update\", \"args\": [{\"visible\": [True] * (2 * len(all_histograms))}]}],\n",
" }\n",
" ]\n",
" ],\n",
")\n",
"\n",
"fig.show()\n"
"fig.show()"
]
},
{
Expand Down Expand Up @@ -4439,27 +4427,29 @@
"\n",
"\n",
"# Function to get color based on p value\n",
"def get_heatmap_color(p, colormap_name='coolwarm', vmin=0.1, vmax=1):\n",
"def get_heatmap_color(p, colormap_name=\"coolwarm\", vmin=0.1, vmax=1):\n",
" colormap = cm.get_cmap(colormap_name)\n",
" norm = LogNorm(vmin=vmin, vmax=vmax) # Normalize p within [vmin, vmax]\n",
" return to_hex(colormap(norm(p))) # Convert to hex color for Plotly\n",
"\n",
"\n",
"# Number of rows and columns for the subplot grid\n",
"num_rows = len(all_histograms) # Each p value gets its own row\n",
"num_cols = 2 # One column for bar chart, one for pie chart\n",
"\n",
"# Create a subplot grid with multiple rows and columns\n",
"fig = make_subplots(\n",
" rows=num_rows, cols=num_cols,\n",
" rows=num_rows,\n",
" cols=num_cols,\n",
" column_widths=[0.7, 0.3],\n",
" row_titles=[f'p={p}' for p in all_histograms.keys()],\n",
" row_titles=[f\"p={p}\" for p in all_histograms.keys()],\n",
" subplot_titles=[\"Output Bitstring Occurrences\", \"Failed/Passed Traps\"] * len(all_histograms),\n",
" vertical_spacing=0.05,\n",
" specs=[[{\"type\": \"bar\"}, {\"type\": \"pie\"}] for _ in range(num_rows)]\n",
" specs=[[{\"type\": \"bar\"}, {\"type\": \"pie\"}] for _ in range(num_rows)],\n",
")\n",
"\n",
"# Add bar charts and pie charts for each p value\n",
"colors = ['#EF553B', '#a5ff33'] # Red for 'Failed traps', Green for 'Passed traps'\n",
"colors = [\"#EF553B\", \"#a5ff33\"] # Red for 'Failed traps', Green for 'Passed traps'\n",
"\n",
"for idx, (p, histogram) in enumerate(all_histograms.items()):\n",
" frequencies = [histogram.get(outcome, 0) for outcome in all_outcomes]\n",
Expand All @@ -4469,35 +4459,32 @@
"\n",
" # Add bar chart to the first column\n",
" fig.add_trace(\n",
" go.Bar(\n",
" x=all_outcomes,\n",
" y=frequencies,\n",
" name=f'Bitstrings (p={p})',\n",
" marker=dict(color=bar_colors)\n",
" ),\n",
" row=idx + 1, col=1\n",
" go.Bar(x=all_outcomes, y=frequencies, name=f\"Bitstrings (p={p})\", marker=dict(color=bar_colors)),\n",
" row=idx + 1,\n",
" col=1,\n",
" )\n",
"\n",
" # Add pie chart to the second column\n",
" failed_value = failed_traps_histograms.get(p, 0)\n",
" fig.add_trace(\n",
" go.Pie(\n",
" labels=['Failed traps', 'Passed traps'],\n",
" labels=[\"Failed traps\", \"Passed traps\"],\n",
" values=[failed_value, 1 - failed_value],\n",
" marker=dict(colors=colors),\n",
" name=f'Traps (p={p})'\n",
" name=f\"Traps (p={p})\",\n",
" ),\n",
" row=idx + 1, col=2\n",
" row=idx + 1,\n",
" col=2,\n",
" )\n",
"\n",
"# Update layout for better spacing and appearance\n",
"fig.update_layout(\n",
" title=\"Output Bitstring Occurrences and Trap Results per Noise Level (p)\",\n",
" height=400 * num_rows, # Dynamic height based on the number of rows\n",
" showlegend=False # Disable legend for clarity\n",
" showlegend=False, # Disable legend for clarity\n",
")\n",
"\n",
"fig.show()\n"
"fig.show()"
]
}
],
Expand Down

0 comments on commit 253e6a2

Please sign in to comment.