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, eps_truncate_imaginary_part=None)[source]
Bases:
quara.objects.qoperation.QOperationConstructor
- 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_requiredtoFalse.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) –
eps_truncate_imaginary_part (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.
ValueError –
is_physicality_requiredisTrueand 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
- 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
- static calc_proj_eq_constraint_with_var(c_sys, var, on_para_eq_constraint=True)[source]
calculates the projection of Gate on equal constraint.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this variables.
var (np.ndarray) – variables.
on_para_eq_constraint (bool, optional) – whether this variables is on parameter equality constraint, by default True.
- Returns
the projection of Gate on equal constraint.
- Return type
np.ndarray
- calc_proj_ineq_constraint()[source]
calculates the projection of QOperation on inequal constraint.
- Returns
the projection of QOperation on inequal constraint.
- Return type
- static calc_proj_ineq_constraint_with_var(c_sys, var, on_para_eq_constraint=True, eps_truncate_imaginary_part=None)[source]
calculates the projection of Gate on inequal constraint.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this variables.
var (np.ndarray) – variables.
on_para_eq_constraint (bool, optional) – whether this variables is on parameter equality constraint, by default True.
eps_truncate_imaginary_part (float, optional) – threshold to truncate imaginary part, by default
get_atol()
- Returns
the projection of Gate on equal constraint.
- Return type
np.ndarray
- 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
- static convert_stacked_vector_to_var(c_sys, stacked_vector, on_para_eq_constraint=True)[source]
converts stacked vector of gate to variables of gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
stacked_vector (np.ndarray) – stacked vector of gate.
on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.
- Returns
variables of gate.
- Return type
np.ndarray
- convert_to_comp_basis(mode='row_major')[source]
returns HS representation for computational basis.
- Parameters
mode (str, optional) – specify whether the order of basis is “row_major” or “column_major”, by default “row_major”.
- Returns
HS representation for computational basis.
- Return type
np.ndarray
- static convert_var_to_stacked_vector(c_sys, var, on_para_eq_constraint=True)[source]
converts variables of gate to stacked vector of gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
var (np.ndarray) – variables of gate.
on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.
- Returns
stacked vector of gate.
- Return type
np.ndarray
- property dim
returns dim of gate.
- Returns
dim of gate.
- Return type
int
- 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_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 checksabsolute(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_choi_matrix_with_dict()[source]
returns Choi matrix of gate.
this function uses the scipy.sparse module.
- Returns
Choi matrix of gate.
- Return type
np.ndarray
- to_choi_matrix_with_sparsity()[source]
returns Choi matrix of gate.
this function uses the scipy.sparse module.
- 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.ndarraywithdtype=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
- 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, eps_truncate_imaginary_part=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]) –
eps_truncate_imaginary_part (Optional[float]) –
- Returns
Gate with gradient as hs.
- Return type
- 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_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
ValueError –
from_hsis not square matrix.ValueError – dim of
from_hsis not square number.ValueError – dim of
from_basisdoes not equal dim ofto_basis.ValueError – length of
from_basisdoes not equal length ofto_basis.
- convert_hs_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_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, eps_truncate_imaginary_part=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]) –
eps_truncate_imaginary_part (Optional[float]) –
- Returns
converted gate.
- Return type
- convert_var_to_hs(c_sys, var, on_para_eq_constraint=True)[source]
converts variables of gate to HS representation.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
var (np.ndarray) – variables of gate.
on_para_eq_constraint (bool, optional) – uses equal constraints, by default True.
- Returns
HS representation of this gate.
- Return type
np.ndarray
- get_amplitutde_damping_channel(gamma, c_sys=None)[source]
- Parameters
gamma (float) –
c_sys (Optional[quara.objects.composite_system.CompositeSystem]) –
- Return type
- get_cnot(c_sys, control)[source]
returns CNOT gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem containing gate.
control (ElementalSystem) – ElementalSystem of control qubit.
- Returns
CNOT gate.
- Return type
- 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
- Raises
ValueError – CompositeSystem is not 2quits.
ValueError – dim of CompositeSystem does not equal 4.
- get_depolarizing_channel(p, c_sys=None)[source]
- Parameters
p (float) –
c_sys (Optional[quara.objects.composite_system.CompositeSystem]) –
- Return type
- get_h(c_sys)[source]
returns H gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem containing gate.
- Returns
H gate.
- Return type
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- Raises
ValueError – CompositeSystem is not 1quit.
ValueError – dim of CompositeSystem does not equal 2
- get_x_rotation(theta, c_sys=None)[source]
- Parameters
theta (float) –
c_sys (Optional[quara.objects.composite_system.CompositeSystem]) –
- Return type
- get_y(c_sys)[source]
returns Pauli Y gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem containing gate.
- Returns
Pauli Y gate.
- Return type
- 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
- Raises
ValueError – CompositeSystem is not 1quit.
ValueError – dim of CompositeSystem does not equal 2
- is_cp(c_sys, hs, atol=None)[source]
returns whether gate is CP(Complete-Positivity-Preserving).
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
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_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 checksabsolute(imaginary part of matrix - zero matrix) <= atol.
- Returns
True where gate is EP, False otherwise.
- Return type
bool
- is_tp(c_sys, hs, atol=None)[source]
returns whether the gate is TP(trace-preserving map).
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
atol (float, optional) – the absolute tolerance parameter, uses
get_atol()by default. this function checksabsolute(trace after mapped - trace before mapped) <= atol.
- Returns
True where the gate is TP, False otherwise.
- Return type
bool
- to_choi_from_hs(c_sys, hs)[source]
converts HS representation to Choi matrix of this gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
- Returns
Choi matrix of this gate.
- Return type
np.ndarray
- to_choi_from_hs_with_dict(c_sys, hs)[source]
converts HS representation to Choi matrix of this gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
- Returns
Choi matrix of this gate.
- Return type
np.ndarray
- to_choi_from_hs_with_sparsity(c_sys, hs)[source]
converts HS representation to Choi matrix of this gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
- Returns
Choi matrix of this gate.
- Return type
np.ndarray
- to_choi_from_var(c_sys, var, on_para_eq_constraint=True)[source]
converts variables to Choi matrix.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
var (np.ndarray) – variables.
on_para_eq_constraint (bool, optional) – whether this gate is on parameter equality constraint, by default True
- Returns
Choi matrix of this gate.
- Return type
np.ndarray
- to_hs_from_choi(c_sys, choi)[source]
converts Choi matrix to HS representation of this gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
choi (np.ndarray) – Choi matrix of this gate.
- Returns
HS representation of this gate.
- Return type
np.ndarray
- to_hs_from_choi_with_dict(c_sys, choi, eps_truncate_imaginary_part=None)[source]
converts Choi matrix to HS representation of this gate.
this function uses dict to calculate fast.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
choi (np.ndarray) – Choi matrix of this gate.
eps_truncate_imaginary_part (float, optional) – threshold to truncate imaginary part, by default
get_atol()
- Returns
HS representation of this gate.
- Return type
np.ndarray
- to_hs_from_choi_with_sparsity(c_sys, choi, eps_truncate_imaginary_part=None)[source]
converts Choi matrix to HS representation of this gate.
this function uses the scipy.sparse module.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
choi (np.ndarray) – Choi matrix of this gate.
eps_truncate_imaginary_part (float, optional) – threshold to truncate imaginary part, by default
get_atol()
- Returns
HS representation of this gate.
- Return type
np.ndarray
- to_hs_from_kraus_matrices(c_sys, kraus, eps_truncate_imaginary_part=None)[source]
returns HS representation of this gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
kraus (List[np.ndarray]) – Kraus matrices of gate.
eps_truncate_imaginary_part (float, optional) – threshold to truncate imaginary part, by default
get_atol()
- Returns
HS representation of this gate.
- Return type
np.ndarray
- to_kraus_matrices_from_hs(c_sys, hs, atol=None)[source]
returns Kraus matrices of gate.
this function returns Kraus matrices as list of
np.ndarraywithdtype=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.- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
atol (float, optional) – the absolute tolerance parameter, uses
get_atol()by default. this function ignores eigenvalues close zero.
- Returns
Kraus matrices of gate.
- Return type
List[np.ndarray]
- to_process_matrix_from_hs(c_sys, hs)[source]
returns process matrix of gate.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
hs (np.ndarray) – HS representation of this gate.
- Returns
process matrix of gate.
- Return type
np.ndarray
- to_var_from_choi(c_sys, choi, on_para_eq_constraint=True)[source]
converts Choi matrix to variables.
- Parameters
c_sys (CompositeSystem) – CompositeSystem of this gate.
choi (np.ndarray) – Choi matrix of this gate.
on_para_eq_constraint (bool, optional) – whether this gate is on parameter equality constraint, by default True
- Returns
variables.
- Return type
np.ndarray