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=True, copy_default_backend=True, copy_cache=None, copy_history=None)[source]

Copy the circuit.

:params copy_backends :bool copy backends if True. copy_default_backend :bool copy default_backend if True.

get_default_backend_name()[source]

Get the default backend of this circuit or global setting.

Returns:
str: The name of default backend.
make_cache(backend=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.

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.
set_default_backend(backend_name)[source]

Set the default backend of this circuit.

This setting is only applied for this circuit. If you want to change the default backend of all gates, use BlueqatGlobalSetting.set_default_backend().

After set the default backend by this method, global setting is ignored even if BlueqatGlobalSetting.set_default_backend() is called. If you want to use global default setting, call this method with backend_name=None.

Args:
backend_name (str or None): new default backend name.
If None is given, global setting is applied.
Raises:
ValueError: If backend_name is not registered backend.
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, params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

2-Controlled Z gate

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

Bases: blueqat.gate.TwoQubitGate

Rotate-X gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

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

Bases: blueqat.gate.TwoQubitGate

Rotate-Y gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

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

Bases: blueqat.gate.TwoQubitGate

Rotate-Z gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'crz'
class blueqat.gate.CU1Gate(targets, lambd, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled U1 gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'cu1'
class blueqat.gate.CU2Gate(targets, phi, lambd, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled U2 gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'cu2'
class blueqat.gate.CU3Gate(targets, theta, phi, lambd, **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Controlled U3 gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'cu3'
class blueqat.gate.CXGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Control-X (CNOT) Gate

lowername = 'cx'
class blueqat.gate.CZGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Control-Z gate

lowername = 'cz'
class blueqat.gate.Gate(targets, params=(), **kwargs)[source]

Bases: abc.ABC

Abstract quantum gate class.

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = None
uppername

Upper name of the gate.

class blueqat.gate.HGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Hadamard Gate

lowername = 'h'
class blueqat.gate.IGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Identity Gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'i'
class blueqat.gate.Measurement(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Measurement gate

lowername = 'measure'
class blueqat.gate.OneQubitGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Abstract quantum gate class for 1 qubit gate.

target_iter(n_qubits)[source]

The generator which yields the target qubits.

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

Bases: blueqat.gate.OneQubitGate

Rotate-X gate

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

Bases: blueqat.gate.OneQubitGate

Rotate-Y gate

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

Bases: blueqat.gate.OneQubitGate

Rotate-Z gate

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

Bases: blueqat.gate.OneQubitGate

Dagger of S gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'sdg'
class blueqat.gate.SGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

S gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 's'
class blueqat.gate.SwapGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.TwoQubitGate

Swap gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'swap'
class blueqat.gate.TDagGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Dagger of T ($pi/8$) gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'tdg'
class blueqat.gate.TGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

T ($pi/8$) gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 't'
class blueqat.gate.ToffoliGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Toffoli (CCX) gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'ccx'
class blueqat.gate.TwoQubitGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.Gate

Abstract quantum gate class for 2 qubits gate.

control_target_iter(n_qubits)[source]

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

class blueqat.gate.U1Gate(targets, lambd, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

U1 gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'u1'
class blueqat.gate.U2Gate(targets, phi, lambd, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

U2 gate

fallback(n_qubits)[source]

Returns alternative gates to make equivalent circuit.

lowername = 'u2'
class blueqat.gate.U3Gate(targets, theta, phi, lambd, **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

U3 gate

lowername = 'u3'
class blueqat.gate.XGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Pauli’s X Gate

lowername = 'x'
class blueqat.gate.YGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Pauli’s Y Gate

lowername = 'y'
class blueqat.gate.ZGate(targets, params=(), **kwargs)[source]

Bases: blueqat.gate.OneQubitGate

Pauli’s Z Gate

lowername = 'z'
blueqat.gate.find_n_qubits(gates)[source]

Find n_qubits from gates

blueqat.gate.get_maximum_index(indices)[source]

Internally used.

blueqat.gate.qubit_pairs(args, length)[source]

Internally used.

blueqat.gate.slicing(args, length)[source]

Internally used.

blueqat.gate.slicing_singlevalue(arg, length)[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 Term.

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)[source]

Convert to the matrix.

static zero()[source]

Returns 0 as Term

is_identity

If self is I, returns True, otherwise False.

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.

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)[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.

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.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(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”.
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_objective(sampler)[source]

Get an objective function to be optimized.

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

Bases: blueqat.vqe.AnsatzBase

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.

Module contents

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

Bases: object

Store the gate operations and call the backends.

copy(copy_backends=True, copy_default_backend=True, copy_cache=None, copy_history=None)[source]

Copy the circuit.

:params copy_backends :bool copy backends if True. copy_default_backend :bool copy default_backend if True.

get_default_backend_name()[source]

Get the default backend of this circuit or global setting.

Returns:
str: The name of default backend.
make_cache(backend=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.

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.
set_default_backend(backend_name)[source]

Set the default backend of this circuit.

This setting is only applied for this circuit. If you want to change the default backend of all gates, use BlueqatGlobalSetting.set_default_backend().

After set the default backend by this method, global setting is ignored even if BlueqatGlobalSetting.set_default_backend() is called. If you want to use global default setting, call this method with backend_name=None.

Args:
backend_name (str or None): new default backend name.
If None is given, global setting is applied.
Raises:
ValueError: If backend_name is not registered backend.
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.