Skip to content

Commit

Permalink
Address feedback: move files to auxilary section + add gate_set input
Browse files Browse the repository at this point in the history
  • Loading branch information
ykharkov committed Mar 29, 2024
1 parent e6321b7 commit c422b9e
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 282 deletions.
87 changes: 87 additions & 0 deletions notebooks/auxilary/Random_Circuit.ipynb
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
}
119 changes: 0 additions & 119 deletions notebooks/textbook/Random_Circuit.ipynb

This file was deleted.

This file was deleted.

This file was deleted.

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
-->
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
Loading

0 comments on commit c422b9e

Please sign in to comment.