-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address feedback: move files to auxilary section + add gate_set input
- Loading branch information
Showing
9 changed files
with
176 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Random Circuit\n", | ||
"Generates a random quantum circuit\n", | ||
"\n", | ||
"<u>Circuit Generation for Testing</u>\n", | ||
"\n", | ||
"Random quantum circuits generator allows to create a diverse set of circuits with a variety of output probability distributions. Users can utilize random circuits to test performance of quantum simulators and QPUs. \n", | ||
"\n", | ||
"<u> Benchmarking quantum compilation stacks</u>\n", | ||
"\n", | ||
"Random circuits sampled from a fixed gates set (as in the example below) are often used for benchmarking performance of quantum compilation passes: circuit mapping / routing and circuit optimization passes. " | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Run on a local simulator" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from braket.devices import LocalSimulator\n", | ||
"from braket.experimental.auxilary_functions.random_circuit import random_circuit\n", | ||
"from braket.circuits.gates import CNot, Rx, Rz, CPhaseShift, XY\n", | ||
"\n", | ||
"# Code here\n", | ||
"local_simulator = LocalSimulator()\n", | ||
"gate_set = [CNot, Rx, Rz, CPhaseShift, XY]\n", | ||
"circuit = random_circuit(num_qubits=5, \n", | ||
" num_gates=30,\n", | ||
" gate_set=gate_set,\n", | ||
" seed=42)\n", | ||
"task = local_simulator.run(circuit, shots=100)\n", | ||
"result = task.result()\n", | ||
"print(\"--Circuit--\")\n", | ||
"print(circuit)\n", | ||
"print(\"\\n--Counts--\")\n", | ||
"print(result.measurement_counts)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.6" | ||
}, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "5904cb9a2089448a2e1aeb5d493d227c9de33e591d7c07e4016fb81e71061a5d" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
src/braket/experimental/algorithms/random_circuit/random_circuit.md
This file was deleted.
Oops, something went wrong.
99 changes: 0 additions & 99 deletions
99
src/braket/experimental/algorithms/random_circuit/random_circuit.py
This file was deleted.
Oops, something went wrong.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
src/braket/experimental/auxilary_functions/random_circuit/random_circuit.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
The `random_circuit` function generates a random quantum circuit using the Amazon Braket SDK. | ||
The function creates a diverse set of circuits that can be used for testing Braket SDK functionality and compiler benchmarking. | ||
<!-- | ||
[metadata-name]: Random Quantum Circuit Generator | ||
[metadata-tags]: Textbook | ||
[metadata-url]: https://github.com/aws-samples/amazon-braket-algorithm-library/tree/main/src/braket/experimental/algorithms/random_circuit | ||
--> |
55 changes: 55 additions & 0 deletions
55
src/braket/experimental/auxilary_functions/random_circuit/random_circuit.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import inspect | ||
import math | ||
import random | ||
from typing import List | ||
|
||
from braket.circuits import Circuit, Instruction, gates | ||
from braket.circuits.gates import CNot, H, S, T | ||
|
||
|
||
def random_circuit( | ||
num_qubits: int, num_gates: int, gate_set: List[Gate] = [CNot, S, T, H], seed=None | ||
) -> Circuit: | ||
""" | ||
Generates a random quantum circuit. | ||
Args: | ||
num_qubits (int): Number of qubits in the circuit. | ||
num_gates (int): Number of instructions (gates) in the circuit. | ||
gate_set (List[Gate]): List of basis gates for the random circuit. | ||
seed (Optional[int]): Random seed for reproducibility (default is None). | ||
Returns: | ||
Circuit: random quantum circuit. | ||
""" | ||
# Set the seed if provided | ||
if seed is not None: | ||
random.seed(seed) | ||
|
||
instructions = [] | ||
for _ in range(num_gates): | ||
gate = random.choice(gate_set) | ||
gate_class = gate.__class__ | ||
gate_qubits = gate_class.fixed_qubit_count() | ||
|
||
# Select random qubits for the gate | ||
qubits = random.sample(range(num_qubits), gate_qubits) | ||
|
||
# Get the constructor's signature to determine required parameters | ||
init_signature = inspect.signature(gate_class.__init__) | ||
|
||
# Calculate the number of parameters (excluding 'self') | ||
num_params = len(init_signature.parameters) - 1 | ||
|
||
# Generate random parameters for the gate in the range [0, 2*pi] | ||
params = [random.uniform(0, 2 * math.pi) for _ in range(num_params)] | ||
|
||
# Create the gate instance | ||
gate = gate_class(*params) | ||
|
||
# Add the gate as an instruction | ||
instructions.append(Instruction(gate, qubits)) | ||
|
||
# Create a circuit with the list of instructions | ||
circuit = Circuit().add(instructions) | ||
return circuit |
Oops, something went wrong.