quara.objects.gate module

class Gate(c_sys, hs, is_physicality_required=True, is_estimation_object=True, on_para_eq_constraint=True, on_algo_eq_constraint=True, on_algo_ineq_constraint=True, mode_proj_order='eq_ineq', eps_proj_physical=None)[source]

Bases: quara.objects.qoperation.QOperation

Constructor

Parameters
  • c_sys (CompositeSystem) – CompositeSystem of this gate.

  • hs (np.ndarray) – HS representation of this gate.

  • is_physicality_required (bool, optional) –

    checks whether the gate is physically wrong, by default True. if at least one of the following conditions is False, the gate is physically wrong:

    • gate is TP(trace-preserving map).

    • gate is CP(Complete-Positivity-Preserving).

    If you want to ignore the above requirements and create a Gate object, set is_physicality_required to False.

  • is_estimation_object (bool) –

  • on_para_eq_constraint (bool) –

  • on_algo_eq_constraint (bool) –

  • on_algo_ineq_constraint (bool) –

  • mode_proj_order (str) –

  • eps_proj_physical (float) –

Raises
  • ValueError – HS representation is not square matrix.

  • ValueError – dim of HS representation is not square number.

  • ValueError – HS representation is not real matrix.

  • ValueError – dim of HS representation does not equal dim of CompositeSystem.

  • ValueErroris_physicality_required is True and the gate is not physically correct.

calc_gradient(var_index)[source]

calculates gradient of QOperation.

this function must be implemented in the subclass.

Parameters

var_index (int) – index of variables to calculate gradient.

Returns

gradient of QOperation.

Return type

QOperation

Raises

NotImplementedError – this function does not be implemented in the subclass.

calc_proj_eq_constraint()[source]

calculates the projection of QOperation on equal constraint.

Returns

the projection of QOperation on equal constraint.

Return type

QOperation

calc_proj_ineq_constraint()[source]

calculates the projection of QOperation on inequal constraint.

Returns

the projection of QOperation on inequal constraint.

Return type

QOperation

convert_basis(other_basis)[source]

returns HS representation for other_basis.

Parameters

other_basis (MatrixBasis) – basis.

Returns

HS representation for other_basis.

Return type

np.ndarray

convert_to_comp_basis()[source]

returns HS representation for computational basis.

Returns

HS representation for computational basis.

Return type

np.ndarray

property dim

returns dim of gate.

Returns

dim of gate.

Return type

int

get_basis()[source]

returns MatrixBasis of gate.

Returns

MatrixBasis of gate.

Return type

MatrixBasis

property hs

returns HS representation of gate.

Returns

HS representation of gate.

Return type

np.ndarray

is_cp(atol=None)[source]

returns whether gate is CP(Complete-Positivity-Preserving).

Parameters

atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default. this function ignores eigenvalues close zero.

Returns

True where gate is CP, False otherwise.

Return type

bool

is_eq_constraint_satisfied(atol=None)[source]
Parameters

atol (Optional[float]) –

is_ineq_constraint_satisfied(atol=None)[source]
Parameters

atol (Optional[float]) –

is_tp(atol=None)[source]

returns whether the gate is TP(trace-preserving map).

Parameters

atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default. this function checks absolute(trace after mapped - trace before mapped) <= atol.

Returns

True where the gate is TP, False otherwise.

Return type

bool

set_zero()[source]

sets parameters to zero.

this function must be implemented in the subclass.

Raises

NotImplementedError – this function does not be implemented in the subclass.

to_choi_matrix()[source]

returns Choi matrix of gate.

Returns

Choi matrix of gate.

Return type

np.ndarray

to_kraus_matrices()[source]

returns Kraus matrices of gate.

this function returns Kraus matrices as list of np.ndarray with dtype=np.complex128. the list is sorted large eigenvalue order. if HS of gate is not CP, then returns empty list because Kraus matrices does not exist.

Returns

Kraus matrices of gate.

Return type

List[np.ndarray]

to_process_matrix()[source]

returns process matrix of gate.

Returns

process matrix of gate.

Return type

np.ndarray

to_stacked_vector()[source]

converts QOperation to stacked vector.

this function must be implemented in the subclass.

Returns

stacked vector representation of QOperation.

Return type

np.ndarray

Raises

NotImplementedError – this function does not be implemented in the subclass.

to_var()[source]

converts QOperation to variables.

this function must be implemented in the subclass.

Returns

variable representation of QOperation.

Return type

np.ndarray

Raises

NotImplementedError – this function does not be implemented in the subclass.

calc_agf(g, u)[source]

returns AGF(Average Gate Fidelity) and g and u.

Parameters
  • g (Gate) – L-TP-CP map.

  • u (Gate) – unitary gate.

Returns

AGF.

Return type

np.float64

Raises

ValueError – HS representation of u is not Hermitian.

calc_gradient_from_gate(c_sys, hs, var_index, is_estimation_object=True, on_para_eq_constraint=True, on_algo_eq_constraint=True, on_algo_ineq_constraint=True, mode_proj_order='eq_ineq', eps_proj_physical=None)[source]

calculates gradient from gate.

Parameters
  • c_sys (CompositeSystem) – CompositeSystem of this gate.

  • hs (np.ndarray) – HS representation of this gate.

  • var_index (int) – variable index.

  • on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.

  • is_estimation_object (bool) –

  • on_algo_eq_constraint (bool) –

  • on_algo_ineq_constraint (bool) –

  • mode_proj_order (str) –

  • eps_proj_physical (Optional[float]) –

Returns

Gate with gradient as hs.

Return type

Gate

convert_gate_index_to_var_index(c_sys, gate_index, on_para_eq_constraint=True)[source]

converts gate index to variable index.

Parameters
  • c_sys (CompositeSystem) – CompositeSystem of this gate.

  • gate_index (Tuple[int, int]) – gate index. first value of tuple is row number of HS representation of this gate. second value of tuple is column number of HS representation of this gate.

  • on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.

Returns

variable index.

Return type

int

convert_gate_to_var(c_sys, hs, on_para_eq_constraint=True)[source]

converts hs of gate to vec of variables.

Parameters
  • c_sys (CompositeSystem) – CompositeSystem of this gate.

  • hs (np.ndarray) – HS representation of this gate.

  • on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.

Returns

vec of variables.

Return type

np.ndarray

convert_hs(from_hs, from_basis, to_basis)[source]

returns HS representation for to_basis

Parameters
  • from_hs (np.ndarray) – HS representation before convert.

  • from_basis (MatrixBasis) – basis before convert.

  • to_basis (MatrixBasis) – basis after convert.

Returns

HS representation for to_basis.

Return type

np.ndarray

Raises
  • ValueErrorfrom_hs is not square matrix.

  • ValueError – dim of from_hs is not square number.

  • ValueError – dim of from_basis does not equal dim of to_basis.

  • ValueError – length of from_basis does not equal length of to_basis.

convert_var_index_to_gate_index(c_sys, var_index, on_para_eq_constraint=True)[source]

converts variable index to gate index.

Parameters
  • c_sys (CompositeSystem) – CompositeSystem of this gate.

  • var_index (int) – variable index.

  • on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.

Returns

gate index. first value of tuple is row number of HS representation of this gate. second value of tuple is column number of HS representation of this gate.

Return type

Tuple[int, int]

convert_var_to_gate(c_sys, var, is_physicality_required=True, is_estimation_object=True, on_para_eq_constraint=True, on_algo_eq_constraint=True, on_algo_ineq_constraint=True, mode_proj_order='eq_ineq', eps_proj_physical=None)[source]

converts vec of variables to gate.

Parameters
  • c_sys (CompositeSystem) – CompositeSystem of this gate.

  • var (np.ndarray) – vec of variables.

  • on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.

  • is_physicality_required (bool) –

  • is_estimation_object (bool) –

  • on_algo_eq_constraint (bool) –

  • on_algo_ineq_constraint (bool) –

  • mode_proj_order (str) –

  • eps_proj_physical (Optional[float]) –

Returns

converted gate.

Return type

Gate

get_amplitutde_damping_channel(gamma, c_sys=None)[source]
Parameters
Return type

quara.objects.gate.Gate

get_cnot(c_sys, control)[source]

returns CNOT gate.

Parameters
Returns

CNOT gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 2quits.

  • ValueError – dim of CompositeSystem does not equal 4.

get_cz(c_sys)[source]

returns CZ gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

CZ gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 2quits.

  • ValueError – dim of CompositeSystem does not equal 4.

get_depolarizing_channel(p, c_sys=None)[source]
Parameters
Return type

quara.objects.gate.Gate

get_h(c_sys)[source]

returns H gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

H gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_i(c_sys)[source]

returns identity gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

identity gate.

Return type

Gate

get_root_x(c_sys)[source]

returns root of X gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

root of X gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_root_y(c_sys)[source]

returns root of Y gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

root of Y gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_s(c_sys)[source]

returns S gate(root of Z).

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

S gate(root of Z).

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_sdg(c_sys)[source]

returns dagger of S gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

dagger of S gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_swap(c_sys)[source]

returns SWAP gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

SWAP gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 2quits.

  • ValueError – dim of CompositeSystem does not equal 4

get_t(c_sys)[source]

returns T gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

T gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_x(c_sys)[source]

returns Pauli X gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

Pauli X gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_x_rotation(theta, c_sys=None)[source]
Parameters
Return type

quara.objects.gate.Gate

get_y(c_sys)[source]

returns Pauli Y gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

Pauli Y gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

get_z(c_sys)[source]

returns Pauli Z gate.

Parameters

c_sys (CompositeSystem) – CompositeSystem containing gate.

Returns

Pauli Z gate.

Return type

Gate

Raises
  • ValueError – CompositeSystem is not 1quit.

  • ValueError – dim of CompositeSystem does not equal 2

hs_from_choi(choi, c_sys)[source]
Parameters

c_sys (quara.objects.composite_system.CompositeSystem) –

Return type

numpy.ndarray

is_hp(hs, basis, atol=None)[source]

returns whether gate is HP(Hermiticity-Preserving).

HP <=> HS on Hermitian basis is real matrix. therefore converts input basis to Pauli basis, and checks whetever converted HS is real matrix.

Parameters
  • hs (np.ndarray) – HS representation of gate.

  • basis (MatrixBasis) – basis of HS representation.

  • atol (float, optional) – the absolute tolerance parameter, uses get_atol() by default. this function checks absolute(imaginary part of matrix - zero matrix) <= atol.

Returns

True where gate is EP, False otherwise.

Return type

bool