-
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 16 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,149 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, TYPE_CHECKING | ||
|
d1ssk marked this conversation as resolved.
Outdated
|
||
|
|
||
| from graphix_ibmq.compiler import IBMQPatternCompiler | ||
| from graphix_ibmq.job import IBMQJob | ||
| from graphix_ibmq.compile_options import IBMQCompileOptions | ||
|
|
||
| from qiskit import QuantumCircuit | ||
| from qiskit_aer.noise import NoiseModel | ||
| from qiskit.providers.backend import BackendV2 | ||
| from qiskit_aer import AerSimulator | ||
| from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager | ||
| from qiskit_ibm_runtime import SamplerV2 as Sampler | ||
|
|
||
| if TYPE_CHECKING: | ||
| from graphix.pattern import Pattern | ||
|
|
||
|
|
||
| class IBMQBackend: | ||
| """IBMQ backend implementation for compiling and executing quantum patterns.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| """Initialize the IBMQ backend.""" | ||
| super().__init__() | ||
|
d1ssk marked this conversation as resolved.
Outdated
|
||
| self._compiler: Optional[IBMQPatternCompiler] = None | ||
|
d1ssk marked this conversation as resolved.
Outdated
d1ssk marked this conversation as resolved.
Outdated
|
||
| self._options: Optional[IBMQCompileOptions] = None | ||
| self._compiled_circuit: Optional[QuantumCircuit] = None | ||
| self._execution_mode: Optional[str] = None | ||
|
|
||
| def compile(self, pattern: Pattern, options: Optional[IBMQCompileOptions] = None) -> None: | ||
| """Compile the assigned pattern using IBMQ options. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| options : CompileOptions, optional | ||
| Compilation options. Must be of type IBMQCompileOptions. | ||
| """ | ||
| if options is None: | ||
| self._options = IBMQCompileOptions() | ||
| elif not isinstance(options, IBMQCompileOptions): | ||
| raise TypeError("Expected IBMQCompileOptions") | ||
| else: | ||
| self._options = options | ||
|
|
||
| self._pattern = pattern | ||
|
|
||
| self._compiler = IBMQPatternCompiler(pattern) | ||
| self._compiled_circuit = self._compiler.to_qiskit_circuit( | ||
| save_statevector=self._options.save_statevector, | ||
| layout_method=self._options.layout_method, | ||
| ) | ||
|
|
||
| def set_simulator( | ||
| self, | ||
| noise_model: Optional[NoiseModel] = None, | ||
| based_on: Optional[BackendV2] = None, | ||
| ) -> None: | ||
| """Configure the backend to use a simulator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| noise_model : NoiseModel, optional | ||
| Noise model to apply to the simulator. | ||
| based_on : BackendV2, optional | ||
| Backend to base the noise model on. | ||
| """ | ||
| if noise_model is None and based_on is not None: | ||
| noise_model = NoiseModel.from_backend(based_on) | ||
|
|
||
| self._execution_mode = "simulation" | ||
| self._noise_model = noise_model | ||
| self._simulator = AerSimulator(noise_model=noise_model) | ||
|
|
||
| def select_device( | ||
| self, | ||
| name: Optional[str] = None, | ||
| least_busy: bool = False, | ||
|
d1ssk marked this conversation as resolved.
Outdated
|
||
| min_qubits: int = 1, | ||
| ) -> None: | ||
| """Select a hardware backend from IBMQ. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| name : str, optional | ||
| Specific device name to use. | ||
| least_busy : bool, optional | ||
| If True, select the least busy device that meets requirements. | ||
| min_qubits : int, optional | ||
| Minimum number of qubits required. | ||
| """ | ||
| from qiskit_ibm_runtime import QiskitRuntimeService | ||
|
|
||
| self._execution_mode = "hardware" | ||
| service = QiskitRuntimeService() | ||
|
|
||
| if least_busy or name is None: | ||
| self._resource = service.least_busy(min_num_qubits=min_qubits, operational=True) | ||
| else: | ||
| self._resource = service.backend(name) | ||
|
|
||
| print(f"Selected device: {self._resource.name}") | ||
|
|
||
| def submit_job(self, shots: int = 1024) -> IBMQJob: | ||
| """Submit the compiled circuit to either simulator or hardware backend. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| shots : int, optional | ||
| Number of execution shots. Defaults to 1024. | ||
|
|
||
| Returns | ||
| ------- | ||
| Job | ||
| A handle to monitor the job status and retrieve results. | ||
|
|
||
| Raises | ||
| ------ | ||
| RuntimeError | ||
| If the pattern has not been compiled or execution mode is not set. | ||
| """ | ||
| if self._compiled_circuit is None: | ||
| raise RuntimeError("Pattern must be compiled before submission.") | ||
|
|
||
| if self._execution_mode is None: | ||
| raise RuntimeError("Execution mode is not configured. Use select_backend() or set_simulator().") | ||
|
|
||
| if self._execution_mode == "simulation": | ||
| pm = generate_preset_pass_manager( | ||
| backend=self._simulator, | ||
| optimization_level=self._options.optimization_level, | ||
|
d1ssk marked this conversation as resolved.
Outdated
|
||
| ) | ||
| transpiled = pm.run(self._compiled_circuit) | ||
| sampler = Sampler(mode=self._simulator) | ||
| job = sampler.run([transpiled], shots=shots) | ||
| return IBMQJob(job, self._compiler) | ||
|
|
||
| elif self._execution_mode == "hardware": | ||
| pm = generate_preset_pass_manager( | ||
| backend=self._resource, | ||
| optimization_level=self._options.optimization_level, | ||
| ) | ||
| transpiled = pm.run(self._compiled_circuit) | ||
| sampler = Sampler(mode=self._resource) | ||
| job = sampler.run([transpiled], shots=shots) | ||
| return IBMQJob(job, self._compiler) | ||
|
|
||
| else: | ||
| raise RuntimeError("Execution mode is not configured. Use select_backend() or set_simulator().") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.