-
Notifications
You must be signed in to change notification settings - Fork 2
Refactoring API structure #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
05ddfc9
refactor API structure
d1ssk cfc7d3d
add docstring
d1ssk cc96047
add tests
d1ssk 94f5506
edit setup.py
d1ssk edd098b
update tox.ini
d1ssk d704a07
update requirement.txt
d1ssk 7be6452
add py3.12, remove py3.8 in test and support
d1ssk 4d695d6
edit ci.yml
d1ssk edf5f22
black
d1ssk 66154e7
update tox.ini
d1ssk 5621900
update tox.ini
d1ssk 9ebab22
remove py3.12 from support and test
d1ssk ddfaff6
remove py3.12 from support and test
d1ssk ad49e67
delete abstract class in graphix
d1ssk c4748e7
update test
d1ssk 9d9adf1
update test
d1ssk b153822
refactor according to the review
d1ssk 409ae3c
unpin dependencies
d1ssk 9cade0e
edit setup and ci
d1ssk 5396ce4
remove python version specification in ci.yml
d1ssk fcbfe34
remove python setup in ci.yml
d1ssk f4353bd
recover ci.yml
d1ssk d2b6035
refactor
d1ssk b655ce0
black
d1ssk aa713a2
Redesign IBMQBackend for improved type safety
d1ssk 8b533a1
reflect the review
d1ssk 71f4359
Ensure type safety
d1ssk c1074ae
black
d1ssk c6f94d1
add test fot job.py
d1ssk ae3882f
black
d1ssk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,126 @@ | ||
| from __future__ import annotations | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from qiskit_aer import AerSimulator | ||
| from qiskit_aer.noise import NoiseModel | ||
| from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager | ||
| from qiskit_ibm_runtime import SamplerV2 as Sampler, QiskitRuntimeService | ||
|
|
||
| from graphix_ibmq.compile_options import IBMQCompileOptions | ||
| from graphix_ibmq.compiler import IBMQPatternCompiler, IBMQCompiledCircuit | ||
| from graphix_ibmq.job import IBMQJob | ||
|
|
||
| if TYPE_CHECKING: | ||
| from graphix.pattern import Pattern | ||
| from qiskit.providers.backend import BackendV2, Backend | ||
|
|
||
|
|
||
| class IBMQBackend: | ||
| """ | ||
| Manages compilation and execution on IBMQ simulators or hardware. | ||
|
|
||
| This class configures the execution target and provides methods to compile | ||
| a graphix Pattern and submit it as a job. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._options = IBMQCompileOptions() | ||
| # The target backend, either a simulator or a real hardware device. | ||
| self._backend: Backend | None = None | ||
|
|
||
| def compile(self, pattern: Pattern, options: IBMQCompileOptions | None = None) -> IBMQCompiledCircuit: | ||
| """ | ||
| Compiles the given pattern into a Qiskit QuantumCircuit. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pattern : Pattern | ||
| The graphix pattern to compile. | ||
| options : IBMQCompileOptions, optional | ||
| Compilation options. If not provided, default options are used. | ||
|
|
||
| Returns | ||
| ------- | ||
| IBMQCompiledCircuit | ||
| An object containing the compiled circuit and related metadata. | ||
| """ | ||
| if options is None: | ||
| self._options = IBMQCompileOptions() | ||
| elif not isinstance(options, IBMQCompileOptions): | ||
| raise TypeError("options must be an instance of IBMQCompileOptions") | ||
| else: | ||
| self._options = options | ||
|
|
||
| compiler = IBMQPatternCompiler(pattern) | ||
| return compiler.compile(save_statevector=self._options.save_statevector) | ||
|
|
||
| def set_simulator(self, noise_model: NoiseModel | None = None, from_backend: BackendV2 | None = None) -> None: | ||
| """ | ||
| Configures the backend to use a local Aer simulator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| noise_model : NoiseModel, optional | ||
| A custom noise model for the simulation. | ||
| from_backend : BackendV2, optional | ||
| A hardware backend to base the noise model on. Ignored if `noise_model` is provided. | ||
| """ | ||
| if noise_model is None and from_backend is not None: | ||
| noise_model = NoiseModel.from_backend(from_backend) | ||
|
|
||
| self._backend = AerSimulator(noise_model=noise_model) | ||
| print("Backend set to local AerSimulator.") | ||
|
|
||
| def set_hardware(self, name: str | None = None, least_busy: bool = False, min_qubits: int = 1) -> None: | ||
| """ | ||
| Selects a real hardware backend from IBM Quantum. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| name : str, optional | ||
| The specific name of the device (e.g., 'ibm_brisbane'). | ||
| least_busy : bool | ||
| If True, selects the least busy device meeting the criteria. | ||
| min_qubits : int | ||
| The minimum number of qubits required. | ||
| """ | ||
| service = QiskitRuntimeService() | ||
|
|
||
| if name: | ||
| backend = service.backend(name) | ||
| else: | ||
| backend = service.least_busy(min_num_qubits=min_qubits, operational=True) | ||
|
|
||
| self._backend = backend | ||
| # Note: In a production library, consider using the `logging` module instead of `print`. | ||
| print(f"Selected hardware backend: {self._backend.name}") | ||
|
|
||
| def submit_job(self, compiled_circuit: IBMQCompiledCircuit, shots: int = 1024) -> IBMQJob: | ||
| """ | ||
| Submits the compiled circuit to the configured backend for execution. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| compiled_circuit : IBMQCompiledCircuit | ||
| The compiled circuit object from the `compile` method. | ||
| shots : int, optional | ||
| The number of execution shots. Defaults to 1024. | ||
|
|
||
| Returns | ||
| ------- | ||
| IBMQJob | ||
| A job object to monitor execution and retrieve results. | ||
| """ | ||
| if self._backend is None: | ||
| raise RuntimeError("Backend not set. Call 'set_simulator()' or 'set_hardware()' before submitting a job.") | ||
|
|
||
| pass_manager = generate_preset_pass_manager( | ||
| backend=self._backend, | ||
| optimization_level=self._options.optimization_level, | ||
| ) | ||
| transpiled_circuit = pass_manager.run(compiled_circuit.circuit) | ||
|
|
||
| sampler = Sampler(mode=self._backend) | ||
| job = sampler.run([transpiled_circuit], shots=shots) | ||
|
|
||
| return IBMQJob(job, compiled_circuit) |
This file contains hidden or 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,30 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
d1ssk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @dataclass | ||
| class IBMQCompileOptions: | ||
| """Compilation options specific to IBMQ backends. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| optimization_level : int | ||
| Optimization level for Qiskit transpiler (0 to 3). | ||
| save_statevector : bool | ||
| Whether to save the statevector before measurement (for debugging/testing). | ||
| layout_method : str | ||
| Qubit layout method used by the transpiler (for future use). | ||
| """ | ||
|
|
||
d1ssk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| optimization_level: int = 3 | ||
| save_statevector: bool = False | ||
| layout_method: str = "trivial" | ||
|
|
||
| def __repr__(self) -> str: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is automatically implemented by |
||
| """Return a string representation of the compilation options.""" | ||
| return ( | ||
| f"IBMQCompileOptions(optimization_level={self.optimization_level}, " | ||
| f"save_statevector={self.save_statevector}, " | ||
| f"layout_method='{self.layout_method}')" | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These versions of Python are not really used: regardless of the version of Python we test here,
toxinstalls and tests Python3.9,3.10, and3.11in its own environments. That means we run the exact same set of tests four times, and each time we test the three versions of Python listed intox.ini. We probably want to test only one version of Python at this level (and probably even without explicitly specifying a particular version), and lettoxrun the tests in its multiple environments.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment, but actually I don't think the current setup runs every tox env four times. Each github actions job only invokes its corresponding tox environment (e.g. the 3.10 runner only runs
py310, the 3.11 runner only runspy311, etc.), although runningtoxin local environment indeed execute tests in all the versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, sorry: I was mistaken, you're right. That is the purpose of the
[gh-actions]section intox.ini. Then everything is fine!