import logging
from topqad_sdk._exceptions import TopQADError
[docs]
class Circuit:
"""A Circuit class representing a quantum circuit.
Args:
id (str): ID of the circuit.
status (str): Status of the circuit.
circuit_name (str): Name of the circuit.
Attributes:
id (str): ID of the circuit.
status (str): Status of the circuit.
circuit_name (str): Name of the circuit.
"""
_logger = logging.getLogger(__name__)
def __init__(
self,
id: str,
status: str,
circuit_name: str,
client=None,
):
"""Initialize Circuit."""
self.id = id
self.circuit_name = circuit_name
self.status = status
self._client = client # Pass CircuitLibrary instance here
self._circuit_path = None
def __repr__(self):
return f"Circuit(id={self.id}, status={self.status}, circuit_name={self.circuit_name})"
@property
def circuit_path(self) -> str:
"""Fetch and return the circuit_path for this circuit.
Returns:
str: The circuit file path.
Raises:
TopQADError: If the circuit cannot be retrieved.
"""
if not self._client:
self._logger.error("No client set for Circuit object.")
raise RuntimeError(
"Unable to retrieve circuit path: no client set for Circuit object."
)
self._logger.info(f"Fetching circuit path for circuit ID: {self.id}")
try:
# Check if the circuit_id exists in examples
examples = self._client.list_all_examples()
if self.id in [circuit.id for circuit in examples]:
example_circuit = self._client.get_example_by_id(self.id)
self._circuit_path = getattr(example_circuit, "_circuit_path", "")
self._logger.debug(
f"Circuit ID {example_circuit.id} identified as an example circuit."
)
return self._circuit_path
# Check if the circuit_id exists in uploads
uploaded_circuits = self._client.list_all_uploads()
if self.id in [circuit.id for circuit in uploaded_circuits]:
uploaded_circuit = self._client.get_uploaded_by_id(self.id)
self._circuit_path = getattr(uploaded_circuit, "_circuit_path", "")
self._logger.debug(
f"Circuit ID {uploaded_circuit.id} identified as an uploaded circuit."
)
return self._circuit_path
except TopQADError as e:
self._logger.error(f"Error while fetching circuit path: {e}")
@property
def as_dict(self) -> dict:
"""Return a dictionary representation of the Circuit object.
Returns:
dict: Dictionary with circuit fields.
"""
return {
"id": self.id,
"status": self.status,
"circuit_name": self.circuit_name,
}
[docs]
class LiteCircuit:
"""A lite circuit class.
The LiteCircuit class provides a basic structure for representing quantum circuits
with only two parameters for just resource estimation.
Args:
num_qubits (int): The number of qubits in the circuit. Must be between 1 and 1,000,000,000.
num_operations (int): The number of operations in the circuit. Must be between 1 and 1e20.
Raises:
ValueError: If num_qubits or num_operations are not within their valid ranges or are not integers.
Attributes:
num_qubits (int): The number of qubits in the circuit.
num_operations (int): The number of operations in the circuit.
"""
def __init__(
self,
num_qubits: int,
num_operations: int,
circuit_name: str = None,
):
if not isinstance(num_qubits, int) or not (0 < num_qubits < 1e9):
raise ValueError(
f"num_qubits: {num_qubits} must be an integer between 1 and 1,000,000,000."
)
if not isinstance(num_operations, int) and not (0 < num_operations < 1e20):
raise ValueError(
f"num_operations:{num_operations} must be an integer between 1 and 1e20."
)
self.circuit_name = circuit_name
self.num_qubits = num_qubits
self.num_operations = num_operations