qpandalite.backend module#
Unified backend management for quantum computing platforms.
This module provides a centralized Backend management system with: - Abstract base class QuantumBackend defining a unified interface - Factory pattern for backend instance creation/retrieval - Caching mechanism for backend instances - Integration with existing adapters (OriginQ, Quafu, IBM)
- Usage:
# Get or create a backend instance backend = get_backend('originq')
# List all available backends available = list_backends()
# Submit a circuit task_id = backend.submit(circuit, shots=1000)
# Query task status result = backend.query(task_id)
- class qpandalite.backend.IBMBackend(name=None, config=None, cache_dir=None)[源代码]
基类:
QuantumBackendBackend for IBM Quantum via Qiskit.
This backend connects to IBM Quantum services for executing quantum circuits on IBM quantum computers and simulators.
- Proxy Configuration:
Proxies can be configured in multiple ways (in priority order): 1. Explicit config dict passed to constructor 2. Environment variables (HTTP_PROXY, HTTPS_PROXY) 3. qpandalite.yml configuration file
示例
>>> # Using config file >>> backend = get_backend('ibm') >>> # Check proxy availability >>> backend.check_proxy() True >>> # Test IBM connectivity >>> result = backend.test_connectivity() >>> print(result['success']) True
- 参数:
name (str | None)
config (dict[str, Any] | None)
cache_dir (Path | str | None)
- check_proxy()[源代码]
Check if the configured proxy is available.
- 返回:
True if proxy is configured and reachable, False otherwise.
- 返回类型:
bool
备注
If no proxy is configured, returns True (direct connection).
- get_proxy_config()[源代码]
Get the current proxy configuration.
- 返回:
Dict with 'http' and/or 'https' proxy URLs, or None if not configured.
- 返回类型:
dict[str, str] | None
- platform: ClassVar[str] = 'ibm'
- test_connectivity()[源代码]
Test connectivity to IBM Quantum services.
- 返回:
- {
'success': bool, 'message': str, 'proxy_used': dict | None, 'response_time_ms': float | None,
}
- 返回类型:
dict with connectivity test results
- class qpandalite.backend.OriginQBackend(name=None, config=None, cache_dir=None)[源代码]
基类:
QuantumBackendBackend for OriginQ Cloud (本源量子云).
This backend connects to the OriginQ Cloud service for executing quantum circuits on OriginQ quantum computers and simulators.
- 参数:
name (str | None)
config (dict[str, Any] | None)
cache_dir (Path | str | None)
- platform: ClassVar[str] = 'originq'
- class qpandalite.backend.QuafuBackend(name=None, config=None, cache_dir=None)[源代码]
基类:
QuantumBackendBackend for BAQIS Quafu (ScQ) quantum cloud platform.
This backend connects to the Quafu service for executing quantum circuits on superconducting quantum computers.
- 参数:
name (str | None)
config (dict[str, Any] | None)
cache_dir (Path | str | None)
- VALID_CHIP_IDS = frozenset({'Dongling', 'ScQ-P10', 'ScQ-P10C', 'ScQ-P136', 'ScQ-P18'})
- platform: ClassVar[str] = 'quafu'
- validate_chip_id(chip_id)[源代码]
Validate if the chip ID is valid for Quafu.
- 参数:
chip_id (str) -- The chip identifier to validate.
- 返回:
True if the chip ID is valid.
- 返回类型:
bool
- class qpandalite.backend.QuantumBackend(name=None, config=None, cache_dir=None)[源代码]
基类:
ABCAbstract base class for quantum backend management.
This class provides a unified interface for all quantum computing backends, wrapping the underlying adapters and providing caching capabilities.
- 参数:
name (str | None)
config (dict[str, Any] | None)
cache_dir (Path | str | None)
- name
The name of this backend instance.
- platform
The platform identifier (e.g., 'originq', 'quafu', 'ibm').
- Type:
ClassVar[str]
- adapter
The underlying quantum adapter instance.
- config
Backend-specific configuration dictionary.
- property adapter: QuantumAdapter
Get or create the underlying adapter instance.
- 返回:
The quantum adapter for this backend.
- 抛出:
RuntimeError -- If the adapter cannot be initialized.
- clear_cache()[源代码]
Clear the cache for this backend instance.
- 返回类型:
None
- get_circuit_adapter()[源代码]
Get the circuit adapter for translating circuits.
- 返回:
The quantum adapter that handles circuit translation.
- 返回类型:
QuantumAdapter
- classmethod get_instance(name=None, config=None, use_cache=True, cache_dir=None)[源代码]
Get or create a backend instance (factory method).
- 参数:
name (str | None) -- Optional name for the instance.
config (dict[str, Any] | None) -- Optional configuration dictionary.
use_cache (bool) -- Whether to use/load cache. Defaults to True.
cache_dir (Path | str | None) -- Optional custom cache directory.
- 返回:
A backend instance.
- 返回类型:
QuantumBackend
- is_available()[源代码]
Check if this backend is available.
- 返回:
True if the backend is properly configured and ready to use.
- 返回类型:
bool
- classmethod list_available()[源代码]
Check if this backend type is available.
- 返回:
True if the backend can be instantiated and is configured.
- 返回类型:
bool
- classmethod load_from_cache(cache_dir=None)[源代码]
Load a backend instance from cache.
- 参数:
cache_dir (Path | str | None) -- Optional custom cache directory path.
- 返回:
Loaded backend instance or None if cache doesn't exist or is invalid.
- 返回类型:
'QuantumBackend' | None
- platform: ClassVar[str] = ''
- query(task_id)[源代码]
Query a task's status and result.
- 参数:
task_id (str) -- Task identifier.
- 返回:
'status': 'success' | 'failed' | 'running'
'result': Execution result (when status is 'success' or 'failed')
- 返回类型:
Dict with keys
- query_batch(task_ids)[源代码]
Query multiple tasks' status and merge results.
- 参数:
task_ids (list[str]) -- List of task identifiers.
- 返回:
'status', 'result' (list of results).
- 返回类型:
Dict with keys
- save_to_cache()[源代码]
Save this backend instance configuration to cache.
- 返回类型:
None
- submit(circuit, *, shots=1000, **kwargs)[源代码]
Submit a circuit to the backend.
- 参数:
circuit (Any) -- Provider-native circuit object or OriginIR string.
shots (int) -- Number of measurement shots.
**kwargs (Any) -- Additional provider-specific parameters.
- 返回:
Task ID assigned by the backend.
- 返回类型:
str
- submit_batch(circuits, *, shots=1000, **kwargs)[源代码]
Submit multiple circuits as a batch.
- 参数:
circuits (list[Any]) -- List of provider-native circuit objects or OriginIR strings.
shots (int) -- Number of measurement shots.
**kwargs (Any) -- Additional provider-specific parameters.
- 返回:
Task ID(s) assigned by the backend.
- 返回类型:
str | list[str]
- translate_circuit(originir)[源代码]
Translate an OriginIR circuit to the platform's native format.
- 参数:
originir (str) -- Circuit in OriginIR format.
- 返回:
Provider-specific circuit object.
- 返回类型:
Any
- qpandalite.backend.get_backend(name, *, config=None, use_cache=True, cache_dir=None)[源代码]
Get or create a backend instance by name.
This is the main factory function for obtaining backend instances. It uses the BACKENDS registry to look up the appropriate backend class and returns a configured instance.
- 参数:
name (str) -- The platform name ('originq', 'quafu', or 'ibm').
config (dict[str, Any] | None) -- Optional configuration dictionary for the backend.
use_cache (bool) -- Whether to use cache. Defaults to True.
cache_dir (Path | str | None) -- Optional custom cache directory path.
- 返回:
A configured QuantumBackend instance.
- 抛出:
ValueError -- If the backend name is not recognized.
RuntimeError -- If the backend cannot be initialized.
- 返回类型:
QuantumBackend
示例
>>> backend = get_backend('originq') >>> task_id = backend.submit(circuit, shots=1000)
- qpandalite.backend.list_backends()[源代码]
List all available backends and their status.
- 返回:
- {
'originq': {'available': True, 'platform': 'originq'}, 'quafu': {'available': False, 'platform': 'quafu'}, ...
}
- 返回类型:
A dictionary mapping backend names to their information
示例
>>> backends = list_backends() >>> for name, info in backends.items(): ... print(f"{name}: {'available' if info['available'] else 'unavailable'}")