Skip to content

Commit

Permalink
Merge pull request #237 from BQSKit/runtime-updates
Browse files Browse the repository at this point in the history
Runtime updates
  • Loading branch information
edyounis authored Aug 28, 2024
2 parents 064d58e + 4749049 commit 52fae4b
Show file tree
Hide file tree
Showing 29 changed files with 1,167 additions and 530 deletions.
99 changes: 17 additions & 82 deletions bqskit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,29 @@
"""The Berkeley Quantum Synthesis Toolkit Python Package."""
from __future__ import annotations

import logging
from sys import stdout as _stdout
from typing import Any

from .version import __version__ # noqa: F401
from .version import __version_info__ # noqa: F401
from bqskit.compiler.compile import compile
from bqskit.compiler.machine import MachineModel
from bqskit.ir.circuit import Circuit
from bqskit.ir.lang import register_language as _register_language
from bqskit.ir.lang.qasm2 import OPENQASM2Language as _qasm
from bqskit._logging import disable_logging
from bqskit._logging import enable_logging
from bqskit._version import __version__ # noqa: F401
from bqskit._version import __version_info__ # noqa: F401

# Initialize Logging
_logging_initialized = False

def __getattr__(name: str) -> Any:
# Lazy imports
if name == 'compile':
from bqskit.compiler.compile import compile
return compile

def enable_logging(verbose: bool = False) -> None:
"""
Enable logging for BQSKit.
if name == 'Circuit':
from bqskit.ir.circuit import Circuit
return Circuit

Args:
verbose (bool): If set to True, will print more verbose messages.
Defaults to False.
"""
global _logging_initialized
if not _logging_initialized:
_logger = logging.getLogger('bqskit')
_handler = logging.StreamHandler(_stdout)
_handler.setLevel(0)
_fmt_header = '%(asctime)s.%(msecs)03d - %(levelname)-8s |'
_fmt_message = ' %(name)s: %(message)s'
_fmt = _fmt_header + _fmt_message
_formatter = logging.Formatter(_fmt, '%H:%M:%S')
_handler.setFormatter(_formatter)
_logger.addHandler(_handler)
_logging_initialized = True
if name == 'MachineModel':
from bqskit.compiler.machine import MachineModel
return MachineModel

level = logging.DEBUG if verbose else logging.INFO
logging.getLogger('bqskit').setLevel(level)


def disable_logging() -> None:
"""Disable logging for BQSKit."""
logging.getLogger('bqskit').setLevel(logging.CRITICAL)


def enable_dashboard() -> None:
import warnings
warnings.warn(
'Dask has been removed from BQSKit. As a result, the'
' enable_dashboard method has been removed.'
'This warning will turn into an error in a future update.',
DeprecationWarning,
)


def disable_dashboard() -> None:
import warnings
warnings.warn(
'Dask has been removed from BQSKit. As a result, the'
' disable_dashboard method has been removed.'
'This warning will turn into an error in a future update.',
DeprecationWarning,
)


def disable_parallelism() -> None:
import warnings
warnings.warn(
'The disable_parallelism method has been removed.'
' Instead, set the "num_workers" parameter to 1 during '
'Compiler construction. This warning will turn into'
'an error in a future update.',
DeprecationWarning,
)


def enable_parallelism() -> None:
import warnings
warnings.warn(
'The enable_parallelism method has been removed.'
' Instead, set the "num_workers" parameter to 1 during '
'Compiler construction. This warning will turn into'
'an error in a future update.',
DeprecationWarning,
)
raise AttributeError(f'module {__name__} has no attribute {name}')


__all__ = [
Expand All @@ -95,6 +33,3 @@ def enable_parallelism() -> None:
'enable_logging',
'disable_logging',
]

# Register supported languages
_register_language('qasm', _qasm())
38 changes: 38 additions & 0 deletions bqskit/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""This module contains the logging configuration and methods for BQSKit."""
from __future__ import annotations

import logging
from sys import stdout as _stdout


_logging_initialized = False


def enable_logging(verbose: bool = False) -> None:
"""
Enable logging for BQSKit.
Args:
verbose (bool): If set to True, will print more verbose messages.
Defaults to False.
"""
global _logging_initialized
if not _logging_initialized:
_logger = logging.getLogger('bqskit')
_handler = logging.StreamHandler(_stdout)
_handler.setLevel(0)
_fmt_header = '%(asctime)s.%(msecs)03d - %(levelname)-8s |'
_fmt_message = ' %(name)s: %(message)s'
_fmt = _fmt_header + _fmt_message
_formatter = logging.Formatter(_fmt, '%H:%M:%S')
_handler.setFormatter(_formatter)
_logger.addHandler(_handler)
_logging_initialized = True

level = logging.DEBUG if verbose else logging.INFO
logging.getLogger('bqskit').setLevel(level)


def disable_logging() -> None:
"""Disable logging for BQSKit."""
logging.getLogger('bqskit').setLevel(logging.CRITICAL)
File renamed without changes.
16 changes: 15 additions & 1 deletion bqskit/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
"""
from __future__ import annotations

from typing import Any

from bqskit.compiler.basepass import BasePass
from bqskit.compiler.compile import compile
from bqskit.compiler.compiler import Compiler
from bqskit.compiler.gateset import GateSet
from bqskit.compiler.gateset import GateSetLike
Expand All @@ -49,6 +50,19 @@
from bqskit.compiler.workflow import Workflow
from bqskit.compiler.workflow import WorkflowLike


def __getattr__(name: str) -> Any:
# Lazy imports
if name == 'compile':
# TODO: fix this (high-priority), overlap between module and function
from bqskit.compiler.compile import compile
return compile

# TODO: Move compile to a different subpackage and deprecate import

raise AttributeError(f'module {__name__} has no attribute {name}')


__all__ = [
'BasePass',
'compile',
Expand Down
7 changes: 3 additions & 4 deletions bqskit/compiler/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import logging
import math
import warnings
from typing import Any
from typing import Literal
Expand All @@ -10,8 +11,6 @@
from typing import TYPE_CHECKING
from typing import Union

import numpy as np

from bqskit.compiler.compiler import Compiler
from bqskit.compiler.machine import MachineModel
from bqskit.compiler.passdata import PassData
Expand Down Expand Up @@ -582,7 +581,7 @@ def type_and_check_input(input: CompilationInputLike) -> CompilationInput:
if error_threshold is not None:
for i, data in enumerate(datas):
error = data.error
nonsq_error = 1 - np.sqrt(max(1 - (error * error), 0))
nonsq_error = 1 - math.sqrt(max(1 - (error * error), 0))
if nonsq_error > error_threshold:
warnings.warn(
'Upper bound on error is greater than set threshold:'
Expand Down Expand Up @@ -631,7 +630,7 @@ def type_and_check_input(input: CompilationInputLike) -> CompilationInput:
# Log error if necessary
if error_threshold is not None:
error = data.error
nonsq_error = 1 - np.sqrt(max(1 - (error * error), 0))
nonsq_error = 1 - math.sqrt(max(1 - (error * error), 0))
if nonsq_error > error_threshold:
warnings.warn(
'Upper bound on error is greater than set threshold:'
Expand Down
Loading

0 comments on commit 52fae4b

Please sign in to comment.