|
| 1 | +# Copyright 2025 Scaleway |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | +import numpy as np |
| 16 | +import random |
| 17 | + |
| 18 | +from qiskit import QuantumCircuit |
| 19 | +from qiskit_scaleway import ScalewayProvider |
| 20 | + |
| 21 | + |
| 22 | +def _random_qiskit_circuit(size: int) -> QuantumCircuit: |
| 23 | + num_qubits = size |
| 24 | + num_gate = size |
| 25 | + |
| 26 | + qc = QuantumCircuit(num_qubits) |
| 27 | + |
| 28 | + for _ in range(num_gate): |
| 29 | + random_gate = np.random.choice(["unitary", "cx", "cy", "cz"]) |
| 30 | + |
| 31 | + if random_gate == "cx" or random_gate == "cy" or random_gate == "cz": |
| 32 | + control_qubit = np.random.randint(0, num_qubits) |
| 33 | + target_qubit = np.random.randint(0, num_qubits) |
| 34 | + |
| 35 | + while target_qubit == control_qubit: |
| 36 | + target_qubit = np.random.randint(0, num_qubits) |
| 37 | + |
| 38 | + getattr(qc, random_gate)(control_qubit, target_qubit) |
| 39 | + else: |
| 40 | + for q in range(num_qubits): |
| 41 | + random_gate = np.random.choice(["h", "x", "y", "z"]) |
| 42 | + getattr(qc, random_gate)(q) |
| 43 | + |
| 44 | + qc.measure_all() |
| 45 | + |
| 46 | + return qc |
| 47 | + |
| 48 | + |
| 49 | +def test_aer_multiple_circuits(): |
| 50 | + provider = ScalewayProvider( |
| 51 | + project_id=os.environ["QISKIT_SCALEWAY_PROJECT_ID"], |
| 52 | + secret_key=os.environ["QISKIT_SCALEWAY_API_TOKEN"], |
| 53 | + url=os.environ["QISKIT_SCALEWAY_API_URL"], |
| 54 | + ) |
| 55 | + |
| 56 | + backend = provider.get_backend("aer_simulation_pop_c16m128") |
| 57 | + |
| 58 | + assert backend is not None |
| 59 | + |
| 60 | + session_id = backend.start_session( |
| 61 | + name="my-aer-session-autotest", |
| 62 | + deduplication_id=f"my-aer-session-autotest-{random.randint(1, 1000)}", |
| 63 | + max_duration="15m", |
| 64 | + ) |
| 65 | + |
| 66 | + assert session_id is not None |
| 67 | + |
| 68 | + try: |
| 69 | + qc1 = _random_qiskit_circuit(20) |
| 70 | + qc2 = _random_qiskit_circuit(15) |
| 71 | + qc3 = _random_qiskit_circuit(21) |
| 72 | + qc4 = _random_qiskit_circuit(17) |
| 73 | + |
| 74 | + run_result = backend.run( |
| 75 | + [qc1, qc2, qc3, qc4], |
| 76 | + shots=1000, |
| 77 | + max_parallel_experiments=0, |
| 78 | + session_id=session_id, |
| 79 | + ).result() |
| 80 | + |
| 81 | + results = run_result.results |
| 82 | + |
| 83 | + assert len(results) == 4 |
| 84 | + |
| 85 | + for result in results: |
| 86 | + assert result.success |
| 87 | + finally: |
| 88 | + backend.delete_session(session_id) |
0 commit comments