blueqat package

Submodules

blueqat.circuit module

This module defines Circuit and the setting for circuit.

class blueqat.circuit.BlueqatGlobalSetting[source]

Bases: object

Setting for Blueqat.

static get_default_backend_name()[source]

Get the default backend name.

Returns:
str: The name of default backend.
static register_backend(name, backend, allow_overwrite=False)[source]

Register new backend.

Args:
name (str): The name of backend.
gateclass (type): The type object of backend
allow_overwrite (bool, optional): If True, allow to overwrite the existing backend. Otherwise, raise the ValueError.
Raises:
ValueError: The name is duplicated with existing backend.
When allow_overwrite=True, this error is not raised.
static register_gate(name, gateclass, allow_overwrite=False)[source]

Register new gate to gate set.

Args:
name (str): The name of gate.
gateclass (type): The type object of gate.
allow_overwrite (bool, optional): If True, allow to overwrite the existing gate.
Otherwise, raise the ValueError.
Raises:
ValueError: The name is duplicated with existing gate.
When allow_overwrite=True, this error is not raised.
static register_macro(name: str, func: Callable, allow_overwrite: bool = False) → None[source]

Register new macro to Circuit.

Args:
name (str): The name of macro.
func (callable): The function to be called.
allow_overwrite (bool, optional): If True, allow to overwrite the existing macro.
Otherwise, raise the ValueError.
Raises:
ValueError: The name is duplicated with existing macro, gate or method.
When allow_overwrite=True, this error is not raised.
static remove_backend(name)[source]

This method is deperecated. Use unregister_backend method.

static set_default_backend(name)[source]

Set the default backend to be used by Circuit. Args:

name (str): The name of the default backend.
Raises:
ValueError: Specified backend is not registered.
static unregister_backend(name)[source]

Unregister a backend.

Args:
name (str): The name of the backend to be unregistered.
Raises:
ValueError: Specified backend is not registered.
static unregister_gate(name)[source]

Unregister a gate from gate set

Args:
name (str): The name of the gate to be unregistered.
Raises:
ValueError: Specified gate is not registered.
static unregister_macro(name)[source]

Unregister a macro.

Args:
name (str): The name of the macro to be unregistered.
Raises:
ValueError: Specified gate is not registered.
class blueqat.circuit.Circuit(n_qubits=0, ops=None)[source]

Bases: object

Store the gate operations and call the backends.

copy(copy_backends: bool = True) → blueqat.circuit.Circuit[source]

Copy the circuit.

params:
copy_backends :bool copy backends if True.
dagger(ignore_measurement: bool = False) → blueqat.circuit.Circuit[source]

Make Hermitian conjugate of the circuit.

This feature is beta. Interface may be changed.

ignore_measurement (bool, optional):
If True, ignore the measurement in the circuit.
Otherwise, if measurement in the circuit, raises ValueError.
make_cache(backend: Optional[BackendUnion] = None) → None[source]

Make a cache to reduce the time of run. Some backends may implemented it.

This is temporary API. It may changed or deprecated.

oneshot(backend: BackendUnion = None, **kwargs) → Tuple[numpy.ndarray, str][source]

Run the circuit and get shots as a result.

run(*args, backend=None, **kwargs)[source]

Run the circuit.

Circuit have several backends. When backend parameter is specified, use specified backend, and otherwise, default backend is used. Other parameters are passed to the backend.

The meaning of parameters are depends on the backend specifications. However, following parameters are commonly used.

Commonly used args (Depends on backend):
shots (int, optional): The number of sampling the circuit.
returns (str, optional): The category of returns value.
e.g. “statevector” returns the state vector after run the circuit.
“shots” returns the counter of measured value.
token, url (str, optional): The token and URL for cloud resource.
Returns:
Depends on backend.
Raises:
Depends on backend.
shots(shots: int, backend: BackendUnion = None, **kwargs) → Counter[str][source]

Run the circuit and get shots as a result.

statevector(backend: BackendUnion = None, **kwargs) → numpy.ndarray[source]

Run the circuit and get a statevector as a result.

to_qasm(*args, **kwargs)[source]

Returns the OpenQASM output of this circuit.

to_unitary(*args, **kwargs)[source]

Returns sympy unitary matrix of this circuit.

blueqat.gate module

gate module implements quantum gate operations. This module is internally used.

class blueqat.gate.CCZGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

2-Controlled Z gate

classmethod create(targets, params) → blueqat.gate.CCZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'ccz'
n_qargs

Number of qubit arguments of this gate.

class blueqat.gate.CHGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled-H gate

classmethod create(targets, params) → blueqat.gate.CHGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

cu_params = (-1.5707963267948966, 3.141592653589793, 0.0, 0.0)
lowername = 'ch'
class blueqat.gate.CPhaseGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-Z gate but phase is different.

classmethod create(targets, params) → blueqat.gate.CPhaseGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'cphase'
class blueqat.gate.CRXGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-X gate

classmethod create(targets, params) → blueqat.gate.CRXGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'crx'
class blueqat.gate.CRYGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-Y gate

classmethod create(targets, params) → blueqat.gate.CRYGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'cry'
class blueqat.gate.CRZGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-Z gate

classmethod create(targets, params) → blueqat.gate.CRZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'crz'
class blueqat.gate.CSwapGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Controlled SWAP gate

classmethod create(targets, params) → blueqat.gate.CSwapGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'cswap'
n_qargs

Number of qubit arguments of this gate.

class blueqat.gate.CUGate(targets, theta: float, phi: float, lam: float, gamma: float = 0.0, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled-U gate.

classmethod create(targets, params) → blueqat.gate.CUGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits: int) → List[blueqat.gate.Gate][source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'cu'
class blueqat.gate.CXGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled-X (CNOT) gate

classmethod create(targets, params) → blueqat.gate.CXGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

cu_params = (3.141592653589793, 0.0, 3.141592653589793, 0.0)
lowername = 'cx'
class blueqat.gate.CYGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled-Y gate

classmethod create(targets, params) → blueqat.gate.CYGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

cu_params = (3.141592653589793, 1.5707963267948966, 1.5707963267948966, 0.0)
lowername = 'cy'
class blueqat.gate.CZGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled-Z gate

classmethod create(targets, params) → blueqat.gate.CZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

cu_params = (0.0, 0.0, 3.141592653589793, 0.0)
lowername = 'cz'
class blueqat.gate.DeprecatedOperation(name: str, alternative: str)[source]

Bases: object

Inform deprecated operation

class blueqat.gate.Gate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Operation

Abstract quantum gate class.

dagger() → blueqat.gate.Gate[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits: int) → List[blueqat.gate.Gate][source]

Returns alternative gates to make equivalent circuit.

matrix() → numpy.ndarray[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

n_qargs

Number of qubit arguments of this gate.

class blueqat.gate.HGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Hadamard gate

classmethod create(targets, params) → blueqat.gate.HGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'h'
u_params = (-1.5707963267948966, 3.141592653589793, 0.0, 0.0)
class blueqat.gate.IGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Identity gate

classmethod create(targets, params) → blueqat.gate.IGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(_)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'i'
u_params = (0.0, 0.0, 0.0, 0.0)
class blueqat.gate.Mat1Gate(targets, mat: numpy.ndarray, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Arbitrary 2x2 matrix gate

mat is expected a 2x2 unitary matrix, but not checked. (If unexpected matrix is given, backend may raises error or returns weird result)

classmethod create(targets, params) → blueqat.gate.Mat1Gate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits: int) → List[blueqat.gate.Gate][source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'mat1'
u_params = None
class blueqat.gate.Measurement(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Operation

Measurement operation

classmethod create(targets, params) → blueqat.gate.Measurement[source]

Create an operation.

target_iter(n_qubits)[source]

The generator which yields the target qubits.

lowername = 'measure'
class blueqat.gate.OneQubitGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Abstract quantum gate class for 1 qubit gate.

fallback(n_qubits: int) → List[blueqat.gate.Gate][source]

Returns alternative gates to make equivalent circuit.

n_qargs

Number of qubit arguments of this gate.

u_params = None

Params for U gate.

class blueqat.gate.Operation(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: object

Abstract quantum circuit operation class.

classmethod create(targets: Union[int, slice, tuple], params: tuple) → _Op[source]

Create an operation.

dagger() → blueqat.gate.Operation[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits: int) → List[blueqat.gate.Operation][source]

Returns alternative operations to make equivalent circuit.

target_iter(n_qubits: int) → Iterator[int][source]

The generator which yields the target qubits.

lowername = ''

Lower name of the operation.

uppername

Upper name of the operation.

class blueqat.gate.PhaseGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Rotate-Z gate but global phase is different.

Global phase doesn’t makes any difference of measured result. You may use RZ gate or U1 gate instead, but distinguishing these gates may better for debugging or future improvement.

furthermore, phase gate may efficient for simulating. (It depends on backend implementation. But matrix of phase gate is simpler than RZ gate or U1 gate.)

classmethod create(targets, params) → blueqat.gate.PhaseGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'phase'
class blueqat.gate.RXGate(targets, theta: float, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Rotate-X gate

classmethod create(targets, params) → blueqat.gate.RXGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'rx'
class blueqat.gate.RXXGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-XX gate

classmethod create(targets, params) → blueqat.gate.RXXGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'rxx'
class blueqat.gate.RYGate(targets, theta: float, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Rotate-Y gate

classmethod create(targets, params) → blueqat.gate.RYGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'ry'
class blueqat.gate.RYYGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-YY gate

classmethod create(targets, params) → blueqat.gate.RYYGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'ryy'
class blueqat.gate.RZGate(targets, theta: float, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Rotate-Z gate

classmethod create(targets, params) → blueqat.gate.RZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'rz'
class blueqat.gate.RZZGate(targets, theta, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Rotate-ZZ gate

classmethod create(targets, params) → blueqat.gate.RZZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'rzz'
class blueqat.gate.Reset(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Operation

Reset operation

classmethod create(targets, params) → blueqat.gate.Reset[source]

Create an operation.

target_iter(n_qubits)[source]

The generator which yields the target qubits.

lowername = 'reset'
class blueqat.gate.SDagGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Dagger of S gate

classmethod create(targets, params) → blueqat.gate.SDagGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'sdg'
u_params = (0.0, 0.0, -1.5707963267948966, 0.0)
class blueqat.gate.SGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

S gate

classmethod create(targets, params) → blueqat.gate.SGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 's'
u_params = (0.0, 0.0, 1.5707963267948966, 0.0)
class blueqat.gate.SXDagGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

sqrt(X)† gate

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'sxdg'
u_params = (-1.5707963267948966, -1.5707963267948966, 1.5707963267948966, -0.7853981633974483)
class blueqat.gate.SXGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

sqrt(X) gate

This is equivalent as RX(π/2) * (1 + i) / √2.

classmethod create(targets, params) → blueqat.gate.SXGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'sx'
u_params = (1.5707963267948966, -1.5707963267948966, 1.5707963267948966, 0.7853981633974483)
class blueqat.gate.SwapGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Swap gate

classmethod create(targets, params) → blueqat.gate.SwapGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'swap'
class blueqat.gate.TDagGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Dagger of T ($pi/8$) gate

classmethod create(targets, params) → blueqat.gate.TDagGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'tdg'
u_params = (0, 0, -0.7853981633974483, 0.0)
class blueqat.gate.TGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

T ($pi/8$) gate

classmethod create(targets, params) → blueqat.gate.TGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 't'
u_params = (0, 0, 0.7853981633974483, 0.0)
class blueqat.gate.ToffoliGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Toffoli (CCX) gate

classmethod create(targets, params) → blueqat.gate.ToffoliGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'ccx'
n_qargs

Number of qubit arguments of this gate.

class blueqat.gate.TwoQubitGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Abstract quantum gate class for 2 qubits gate.

control_target_iter(n_qubits: int) → Iterator[Tuple[int, int]][source]

The generator which yields the tuples of (control, target) qubits.

fallback(n_qubits: int) → List[blueqat.gate.Gate][source]

Returns alternative gates to make equivalent circuit.

n_qargs

Number of qubit arguments of this gate.

class blueqat.gate.UGate(targets, theta: float, phi: float, lam: float, gamma: float = 0.0, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Arbitrary 1 qubit unitary gate including global phase.

U(θ, φ, λ, γ = 0.0) = e^iγ * array([
[cos(θ/2), -e^iλ sin(θ/2)], [e^iφ sin(θ/2), e^i(φ+λ) cos(θ/2)]])

Note: If SU matrix is required, U(θ, φ, λ, (φ + λ) / 2) works fine.

classmethod create(targets, params) → blueqat.gate.UGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits: int) → List[blueqat.gate.Gate][source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'u'
class blueqat.gate.XGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Pauli’s X gate

classmethod create(targets, params) → blueqat.gate.XGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'x'
u_params = (3.141592653589793, 0.0, 3.141592653589793, 0.0)
class blueqat.gate.YGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Pauli’s Y gate

classmethod create(targets, params) → blueqat.gate.YGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'y'
u_params = (3.141592653589793, 1.5707963267948966, 1.5707963267948966, 0.0)
class blueqat.gate.ZGate(targets: Union[int, slice, tuple], params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Pauli’s Z gate

classmethod create(targets, params) → blueqat.gate.ZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'z'
u_params = (0.0, 0.0, 3.141592653589793, 0.0)
class blueqat.gate.ZZGate(targets, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

ZZ gate

This gate is a basis two-qubit gate for some kinds of trapped-ion based machines. It is equivalent with RZZ(pi/2) except global phase.

classmethod create(targets, params) → blueqat.gate.ZZGate[source]

Create an operation.

dagger()[source]

Returns the Hermitian conjugate of self.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

matrix()[source]

Returns the matrix of implementations.

(Non-abstract) subclasses of Gate must implement this method. WARNING: qubit order specifications of multi qubit gate is still not defined.

lowername = 'zz'
blueqat.gate.find_n_qubits(gates: Iterable[blueqat.gate.Operation]) → int[source]

Find n_qubits from gates

blueqat.gate.get_maximum_index(indices: Union[int, slice, tuple]) → int[source]

Internally used.

blueqat.gate.qubit_pairs(args: Tuple[Union[int, slice, tuple], Union[int, slice, tuple]], length: int) → Iterator[Tuple[int, int]][source]

Internally used.

blueqat.gate.slicing(args: Union[int, slice, tuple], length: int) → Iterator[int][source]

Internally used.

blueqat.gate.slicing_singlevalue(arg: Union[slice, int], length: int) → Iterator[int][source]

Internally used.

blueqat.pauli module

The module for calculate Pauli matrices.

class blueqat.pauli.Expr[source]

Bases: blueqat.pauli._ExprTuple

coeffs()[source]

Generator which yields a coefficent for each Term.

commutator(other)[source]

Returns commutator.

static from_number(num)[source]

Make new Expr from a number

static from_term(term)[source]

Make new Expr from a Term

static from_terms_dict(terms_dict)[source]

For internal use.

static from_terms_iter(terms)[source]

For internal use.

is_all_terms_commutable()[source]

Test whether all terms are commutable. This function may very slow.

is_commutable_with(other)[source]

Test whether self is commutable with other.

max_n()[source]

Returns the maximum index of Pauli matrices in the Expr. If Expr is empty or only constant and identity matrix, returns -1.

simplify()[source]

Simplify the Expr.

terms_to_dict()[source]

For internal use.

to_expr()[source]

Do nothing. This method is prepared to avoid TypeError.

to_matrix(n_qubits=-1, *, sparse=None)[source]

Convert to the matrix.

static zero()[source]

Returns 0 as Term

is_identity

If self is I, returns True, otherwise False.

n_qubits

Returns the number of qubits of the Term.

If Expr is empty or only constant and identity matrix, returns 0.

class blueqat.pauli.Term[source]

Bases: blueqat.pauli._TermTuple

Multiplication of Pauli matrices with coefficient. Note that this class is immutable.

Multiplied Pauli matrices are very important for quantum computation because it is an unitary matrix (without coefficient) and also it can be consider the time evolution of the term (with real coefficient) without Suzuki-Trotter expansion.

append_to_circuit(circuit, simplify=True)[source]

Append Pauli gates to Circuit.

commutator(other)[source]

Returns commutator.

static from_chars(chars)[source]

Make Pauli’s Term from chars which is written by “X”, “Y”, “Z” or “I”. e.g. “XZIY” => X(0) * Z(1) * Y(3)

Args:
chars (str): Written in “X”, “Y”, “Z” or “I”.
Returns:
Term: A Term object.
Raises:
ValueError: When chars conteins the character which is “X”, “Y”, “Z” nor “I”.
static from_ops_iter(ops, coeff)[source]

For internal use.

static from_pauli(pauli, coeff=1.0)[source]

Make new Term from an Pauli operator

static from_paulipair(pauli1, pauli2)[source]

Make new Term from two Pauli operator.

get_time_evolution()[source]

Get the function to append the time evolution of this term.

Returns:
function(circuit: Circuit, t: float):
Add gates for time evolution to circuit with time t
is_commutable_with(other)[source]

Test whether self is commutable with other.

static join_ops(ops1, ops2)[source]

For internal use.

max_n()[source]

Returns the maximum index of Pauli matrices in the Term. If there’s no Pauli matrices, returns -1.

n_iter()[source]

Returns an iterator which yields indices for each Pauli matrices in the Term.

simplify()[source]

Simplify the Term.

to_expr()[source]

Convert to Expr.

to_matrix(n_qubits=-1, *, sparse=None)[source]

Convert to the matrix.

to_term()[source]

Do nothing. This method is prepared to avoid TypeError.

is_identity

If self is I, returns True, otherwise False.

n_qubits

Returns the number of qubits of the term. If the term is constant with identity matrix, n_qubits is 0.

blueqat.pauli.commutator(expr1, expr2)[source]

Returns [expr1, expr2] = expr1 * expr2 - expr2 * expr1.

Args:
expr1 (Expr, Term or Pauli operator): Pauli’s expression.
expr2 (Expr, Term or Pauli operator): Pauli’s expression.
Returns:
Expr: expr1 * expr2 - expr2 * expr1.
blueqat.pauli.from_qubo(qubo: Sequence[Sequence[float]]) → blueqat.pauli.Expr[source]

Convert to pauli operators of universal gate model.

blueqat.pauli.is_commutable(expr1, expr2, eps=1e-08)[source]

Test whether expr1 and expr2 are commutable.

Args:
expr1 (Expr, Term or Pauli operator): Pauli’s expression.
expr2 (Expr, Term or Pauli operator): Pauli’s expression.
eps (float, optional): Machine epsilon. If | [expr1, expr2 ]| < eps, consider it is commutable.
Returns:
bool: if expr1 and expr2 are commutable, returns True, otherwise False.
blueqat.pauli.pauli_from_char(ch, n=0)[source]

Make Pauli matrix from an character.

Args:
ch (str): “X” or “Y” or “Z” or “I”.
n (int, optional): Make Pauli matrix as n-th qubits.
Returns:
If ch is “X” => X, “Y” => Y, “Z” => Z, “I” => I
Raises:
ValueError: When ch is not “X”, “Y”, “Z” nor “I”.
blueqat.pauli.qubo_bit(n)[source]

Represent QUBO’s bit to Pauli operator of Ising model.

Args:
n (int): n-th bit in QUBO
Returns:
Expr: Pauli expression of QUBO bit.
blueqat.pauli.term_from_chars(chars)[source]

Make Pauli’s Term from chars which is written by “X”, “Y”, “Z” or “I”. e.g. “XZIY” => X(3) * Z(2) * Y(0)

Args:
chars (str): Written in “X”, “Y”, “Z” or “I”.
Returns:
Term: A Term object.
Raises:
ValueError: When chars conteins the character which is “X”, “Y”, “Z” nor “I”.
blueqat.pauli.to_expr(term)[source]

Convert to Expr from Term or Pauli operator (X, Y, Z, I).

Args:
term: (Term, X, Y, Z or I): A Term or Pauli operator.
Returns:
Expr: An Expr object.
blueqat.pauli.to_term(pauli)[source]

Convert to Term from Pauli operator (X, Y, Z, I).

Args:
pauli (X, Y, Z or I): A Pauli operator
Returns:
Term: A Term object.

blueqat.vqe module

class blueqat.vqe.AnsatzBase(hamiltonian, n_params)[source]

Bases: object

get_circuit(params)[source]

Make a circuit from parameters.

get_energy(circuit, sampler)[source]

Calculate energy from circuit and sampler.

get_energy_sparse(circuit)[source]

Get energy using sparse matrix. This method may be changed in the future release.

get_objective(sampler=None)[source]

Get an objective function to be optimized.

make_sparse(fmt='csc', make_method=None)[source]

Make sparse matrix. This method may be changed in the future release.

class blueqat.vqe.QaoaAnsatz(hamiltonian, step=1, init_circuit=None, mixer=None)[source]

Bases: blueqat.vqe.AnsatzBase

Ansatz for QAOA.

check_hamiltonian()[source]

Check hamiltonian is commutable. This condition is required for QaoaAnsatz

get_circuit(params)[source]

Make a circuit from parameters.

class blueqat.vqe.Vqe(ansatz, minimizer=None, sampler=None)[source]

Bases: object

run(verbose=False)[source]
result

Vqe.result is deprecated. Use result = Vqe.run().

class blueqat.vqe.VqeResult(vqe=None, params=None, circuit=None)[source]

Bases: object

get_probs(sampler=None, rerun=None, store=True)[source]

Get probabilities.

most_common(n=1)[source]
probs

Get probabilities. This property is obsoleted. Use get_probs().

blueqat.vqe.expect(qubits, meas)[source]

For the VQE simulation without sampling.

blueqat.vqe.get_measurement_sampler(n_sample, run_options=None)[source]

Returns a function which get the expectations by sampling the measured circuit

blueqat.vqe.get_qiskit_sampler(backend, **execute_kwargs)[source]

Returns a function which get the expectation by sampling via Qiskit.

This function requires qiskit module.

blueqat.vqe.get_scipy_minimizer(**kwargs)[source]

Get minimizer which uses scipy.optimize.minimize

blueqat.vqe.get_state_vector_sampler(n_sample)[source]

Returns a function which get the expectations by sampling the state vector

blueqat.vqe.non_sampling_sampler(circuit, meas)[source]

Calculate the expectations without sampling.

blueqat.vqe.sparse_expectation(mat, vec)[source]

Calculate expectation value <vec|mat|vec>.

Args:
mat (scipy sparse matrix): Sparse matrix vec (numpy array): Vector
Returns:
(Real part of) expectation value <vec|mat|vec>. Remarks: when mat is Hermitian, <vec|mat|vec> is real.

blueqat.wq module

Module contents

class blueqat.Circuit(n_qubits=0, ops=None)[source]

Bases: object

Store the gate operations and call the backends.

copy(copy_backends: bool = True) → blueqat.circuit.Circuit[source]

Copy the circuit.

params:
copy_backends :bool copy backends if True.
dagger(ignore_measurement: bool = False) → blueqat.circuit.Circuit[source]

Make Hermitian conjugate of the circuit.

This feature is beta. Interface may be changed.

ignore_measurement (bool, optional):
If True, ignore the measurement in the circuit.
Otherwise, if measurement in the circuit, raises ValueError.
make_cache(backend: Optional[BackendUnion] = None) → None[source]

Make a cache to reduce the time of run. Some backends may implemented it.

This is temporary API. It may changed or deprecated.

oneshot(backend: BackendUnion = None, **kwargs) → Tuple[numpy.ndarray, str][source]

Run the circuit and get shots as a result.

run(*args, backend=None, **kwargs)[source]

Run the circuit.

Circuit have several backends. When backend parameter is specified, use specified backend, and otherwise, default backend is used. Other parameters are passed to the backend.

The meaning of parameters are depends on the backend specifications. However, following parameters are commonly used.

Commonly used args (Depends on backend):
shots (int, optional): The number of sampling the circuit.
returns (str, optional): The category of returns value.
e.g. “statevector” returns the state vector after run the circuit.
“shots” returns the counter of measured value.
token, url (str, optional): The token and URL for cloud resource.
Returns:
Depends on backend.
Raises:
Depends on backend.
shots(shots: int, backend: BackendUnion = None, **kwargs) → Counter[str][source]

Run the circuit and get shots as a result.

statevector(backend: BackendUnion = None, **kwargs) → numpy.ndarray[source]

Run the circuit and get a statevector as a result.

to_qasm(*args, **kwargs)[source]

Returns the OpenQASM output of this circuit.

to_unitary(*args, **kwargs)[source]

Returns sympy unitary matrix of this circuit.

class blueqat.BlueqatGlobalSetting[source]

Bases: object

Setting for Blueqat.

static get_default_backend_name()[source]

Get the default backend name.

Returns:
str: The name of default backend.
static register_backend(name, backend, allow_overwrite=False)[source]

Register new backend.

Args:
name (str): The name of backend.
gateclass (type): The type object of backend
allow_overwrite (bool, optional): If True, allow to overwrite the existing backend. Otherwise, raise the ValueError.
Raises:
ValueError: The name is duplicated with existing backend.
When allow_overwrite=True, this error is not raised.
static register_gate(name, gateclass, allow_overwrite=False)[source]

Register new gate to gate set.

Args:
name (str): The name of gate.
gateclass (type): The type object of gate.
allow_overwrite (bool, optional): If True, allow to overwrite the existing gate.
Otherwise, raise the ValueError.
Raises:
ValueError: The name is duplicated with existing gate.
When allow_overwrite=True, this error is not raised.
static register_macro(name: str, func: Callable, allow_overwrite: bool = False) → None[source]

Register new macro to Circuit.

Args:
name (str): The name of macro.
func (callable): The function to be called.
allow_overwrite (bool, optional): If True, allow to overwrite the existing macro.
Otherwise, raise the ValueError.
Raises:
ValueError: The name is duplicated with existing macro, gate or method.
When allow_overwrite=True, this error is not raised.
static remove_backend(name)[source]

This method is deperecated. Use unregister_backend method.

static set_default_backend(name)[source]

Set the default backend to be used by Circuit. Args:

name (str): The name of the default backend.
Raises:
ValueError: Specified backend is not registered.
static unregister_backend(name)[source]

Unregister a backend.

Args:
name (str): The name of the backend to be unregistered.
Raises:
ValueError: Specified backend is not registered.
static unregister_gate(name)[source]

Unregister a gate from gate set

Args:
name (str): The name of the gate to be unregistered.
Raises:
ValueError: Specified gate is not registered.
static unregister_macro(name)[source]

Unregister a macro.

Args:
name (str): The name of the macro to be unregistered.
Raises:
ValueError: Specified gate is not registered.
blueqat.circuitmacro(func: Union[Callable, str, None] = None, *, allow_overwrite: bool = True) → Callable[source]

@circuitmacro decorator.

Typical usage: Case 1: no arguments

@def_macro def egg(c):

equivalent to this:

def egg(c):

BlueqatGlobalSetting.register_macro(‘egg’, egg, allow_overwrite=True)

Case 2: with name:

@def_macro(‘bacon’) def egg(c):

is equivalent with

def egg(c):

BlueqatGlobalSetting.register_macro(‘bacon’, egg, allow_overwrite=True)

Case 3: with allow_overwrite keyword argument

@def_macro(allow_overwrite=False) def egg(c):

or

@def_macro(‘bacon’, allow_overwrite=False) def bacon(c):

call BlueqatGlobalSetting.register_macro with allow_overwrite=False.

Please note that allow_overwrite=True is default behavior. It is convenient for interactive environment likes Jupyter Notebook. However, if you’re library developer, using allow_overwrite=False is hardly recommended.