Spaces:
Runtime error
Runtime error
Download src/scripts/generate_random_circuits.py from QuantumGenAI/ErrorMitigation: direct link, hf CLI and curl.
- Browser
- Download file 7.33 kB
-
https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/scripts/generate_random_circuits.py
- Command line
-
hf download hf://spaces/QuantumGenAI/ErrorMitigation/src/scripts/generate_random_circuits.py
-
curl -L -o generate_random_circuits.py https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/scripts/generate_random_circuits.py
7.33 kB
| import random | |
| from typing import Callable, Optional, Any | |
| import pennylane as qml | |
| from pennylane import numpy as np | |
| from quantum_circuits.circuit_manager import CircuitManager | |
| ############################################################################## | |
| # Static and Dynamic Circuit Definitions | |
| ############################################################################## | |
| def circuit_1(params: Any, n_qubits: int = 3) -> None: | |
| """RX, RZ, RY rotations on 3 qubits.""" | |
| if n_qubits != 3: | |
| raise ValueError("circuit_1 requires exactly 3 qubits") | |
| qml.RX(params[0], wires=0) | |
| qml.RZ(params[1], wires=1) | |
| qml.RY(params[2], wires=2) | |
| def circuit_2(params: Any, n_qubits: int = 2) -> None: | |
| """Bell state circuit for 2 qubits with an extra rotation for nontrivial output.""" | |
| if n_qubits != 2: | |
| raise ValueError("circuit_2 requires exactly 2 qubits") | |
| qml.Hadamard(wires=0) | |
| qml.CNOT(wires=[0, 1]) | |
| qml.RZ(0.15, wires=0) | |
| def circuit_3(params: Any, n_qubits: int = 2) -> None: | |
| """Entangled state with rotation on qubit 0 for 2 qubits.""" | |
| if n_qubits != 2: | |
| raise ValueError("circuit_3 requires exactly 2 qubits") | |
| qml.Hadamard(wires=0) | |
| qml.CNOT(wires=[0, 1]) | |
| qml.RX(params[0] + 0.2, wires=0) | |
| def circuit_4(params: Any, n_qubits: int = 1) -> None: | |
| """Arbitrary single-qubit rotations for 1 qubit.""" | |
| if n_qubits != 1: | |
| raise ValueError("circuit_4 requires exactly 1 qubit") | |
| qml.Rot(params[0], params[1], params[2], wires=0).decomposition() | |
| def circuit_5(params: Any, n_qubits: int) -> None: | |
| """Generalized GHZ state for n_qubits (n_qubits>=2).""" | |
| if n_qubits < 2: | |
| raise ValueError("GHZ state requires at least 2 qubits") | |
| qml.Hadamard(wires=0) | |
| for i in range(1, n_qubits): | |
| qml.CNOT(wires=[0, i]) | |
| def circuit_6(params: Any, n_qubits: int = 3) -> None: | |
| """Variational Ansatz using BasicEntanglerLayers on 3 qubits.""" | |
| params = [np.random.rand(n_qubits, n_qubits)] | |
| wires = list(range(n_qubits)) | |
| qml.templates.BasicEntanglerLayers(params, wires=wires).decomposition() | |
| def circuit_7(params: Any, n_qubits: int = 3) -> None: | |
| """Quantum Fourier Transform (QFT) on 3 qubits.""" | |
| wires = list(range(n_qubits)) | |
| qml.QFT(wires=wires).decomposition() | |
| def circuit_8(params: Any, n_qubits: int = 2) -> None: | |
| """Amplitude Embedding on 2 qubits.""" | |
| wires = list(range(n_qubits)) | |
| qml.templates.AmplitudeEmbedding( | |
| params, wires=wires, normalize=True | |
| ).decomposition() | |
| def circuit_9(params: Any, n_qubits: int = 3) -> None: | |
| """Angle Embedding on 3 qubits.""" | |
| wires = list(range(n_qubits)) | |
| qml.templates.AngleEmbedding(params, wires=wires).decomposition() | |
| def circuit_10(params: Any, n_qubits: int = 3) -> None: | |
| """Strongly Entangling Layers on 3 qubits.""" | |
| wires = list(range(n_qubits)) | |
| qml.templates.StronglyEntanglingLayers(params, wires=wires).decomposition() | |
| def create_dynamic_temp_circuit(num_qubits: int) -> Callable: | |
| """ | |
| Returns a dynamic circuit function for a given number of qubits. | |
| The circuit consists of four layers: | |
| 1. Parameterized single-qubit rotations (RX, RY, RZ) on each qubit. | |
| 2. An entangling layer: a ring of CNOT gates connecting neighboring qubits. | |
| 3. Additional single-qubit rotations for variability. | |
| 4. An entangling layer using CZ gates on alternating pairs. | |
| Args: | |
| num_qubits: Number of qubits for the circuit. | |
| Returns: | |
| A circuit function that accepts parameters and an optional n_qubits argument. | |
| """ | |
| def dynamic_circuit(params: Any, n_qubits: Optional[int] = None) -> None: | |
| n = num_qubits if n_qubits is None else n_qubits | |
| # Layer 1: Parameterized rotations. | |
| for i in range(n): | |
| rx_angle = ( | |
| params[3 * i] if len(params) > 3 * i else random.uniform(0, 2 * np.pi) | |
| ) | |
| ry_angle = ( | |
| params[3 * i + 1] | |
| if len(params) > 3 * i + 1 | |
| else random.uniform(0, 2 * np.pi) | |
| ) | |
| rz_angle = ( | |
| params[3 * i + 2] | |
| if len(params) > 3 * i + 2 | |
| else random.uniform(0, 2 * np.pi) | |
| ) | |
| qml.RX(rx_angle, wires=i) | |
| qml.RY(ry_angle, wires=i) | |
| qml.RZ(rz_angle, wires=i) | |
| # Layer 2: Ring of CNOT gates. | |
| for i in range(n): | |
| if random.random() > 0.5: | |
| qml.CNOT(wires=[i, (i + 1) % n]) | |
| # Layer 3: Additional single-qubit rotations. | |
| for i in range(n): | |
| qml.RZ(random.uniform(0, 2 * np.pi), wires=i) | |
| qml.RX(random.uniform(0, 2 * np.pi), wires=i) | |
| # Layer 4: CZ gates on alternating pairs. | |
| if n >= 2: | |
| for i in range(0, n - 1, 2): | |
| qml.CZ(wires=[i, i + 1]) | |
| return dynamic_circuit | |
| ############################################################################## | |
| # Build a Pool of Circuits | |
| ############################################################################## | |
| # Static circuits (predefined with fixed qubit counts) | |
| static_circuits = [ | |
| (circuit_1, [np.array([0.1, 0.2, 0.3])], "Circuit_1 (Static, 3 qubits)", [0, 1, 2]), | |
| (circuit_2, [np.array([])], "Circuit_2 (Static, 2 qubits)", [0, 1]), | |
| (circuit_3, [np.array([0.5])], "Circuit_3 (Static, 2 qubits)", [0, 1]), | |
| (circuit_4, [np.random.rand(3)], "Circuit_4 (Static, 1 qubit)", [0]), | |
| ( | |
| circuit_8, | |
| [np.random.rand(2**2)], | |
| "Circuit_8 (Static, Amplitude Embedding, 2 qubits)", | |
| [0, 1], | |
| ), | |
| ( | |
| circuit_8, | |
| [np.random.rand(2**3)], | |
| "Circuit_8 (Static, Amplitude Embedding, 3 qubits)", | |
| [0, 1, 2], | |
| ), | |
| ( | |
| circuit_10, | |
| [np.random.rand(2, 3, 3)], | |
| "Circuit_10 (Static, Strongly Entangling, 3 qubits)", | |
| [0, 1, 2], | |
| ), | |
| ( | |
| circuit_10, | |
| [np.random.rand(2, 4, 3)], | |
| "Circuit_10 (Static, Strongly Entangling, 4 qubits)", | |
| [0, 1, 2, 3], | |
| ), | |
| ( | |
| circuit_10, | |
| [np.random.rand(2, 5, 3)], | |
| "Circuit_10 (Static, Strongly Entangling, 5 qubits)", | |
| [0, 1, 2, 3, 4], | |
| ), | |
| ] | |
| # Useful circuits that are applicable for dynamic qubit counts. | |
| useful_circuits = [circuit_5, circuit_6, circuit_7, circuit_9] | |
| # Build the complete circuits list. | |
| circuits = [] | |
| circuits.extend(static_circuits) | |
| # Add dynamic circuits and apply useful circuits for wire counts from 2 to 12. | |
| for n in range(2, 13): | |
| wires = list(range(n)) | |
| dynamic_circ = create_dynamic_temp_circuit(n) | |
| dynamic_params = np.random.rand(n) | |
| circuits.append( | |
| (dynamic_circ, [dynamic_params], f"Dynamic_Circuit_{n}_qubits", wires) | |
| ) | |
| # For each useful circuit, use the same parameter vector. | |
| for circuit in useful_circuits: | |
| circuits.append((circuit, [dynamic_params], circuit.__name__, wires)) | |
| def main(): | |
| device_name = "default.mixed" | |
| use_qasm = True | |
| manager = CircuitManager( | |
| db_path="quantum_circuits.duckdb", | |
| use_qasm=use_qasm, | |
| device_name=device_name, | |
| ) | |
| # Run each circuit with 10 noisy variations. | |
| for circuit_func, params, name, wires in circuits: | |
| manager.run_circuit(circuit_func, params, name, wires, num_variations=10) | |