qtree package¶
qtree.qtree¶
Quantum decision trees.
- class qtree.qtree.QTree(max_depth, quantum_backend=None, d_weight=1.0, s_weight=1.0, a_weight=1.0, pareto_fitness=False, score_func=None, dist_func=None, acc_func=None, swap_func=None, mcswap_func=None, mct_func=None, pop_size=10, cx_pb=0.2, mut_pb=0.2, mut_ind_pb=0.2, select_tournsize=3, max_generations=10, reuse_G_calc=True, mean_fits=True, random_state=None, evo_callback=None, remove_inactive_qubits=True, eliminate_first_swap=False, simultaneous_circuit_evaluation=False, include_id=False, block_form=False, use_barriers=False, logger=None)[source]¶
Bases:
QTreeBaseMain class for quantum decision classifiers.
- Parameters:
max_depth (int) – Maximum depth of the decision tree. A tree of depth 0 consists only of the root.
quantum_backend (qiskit.providers.Backend or None) – Qiskit quantum backend used for all calculations. Can be either a simulator or an actual backend. If set to
None, useAerSimulator(). Defaults toNone.d_weight (float) – Hyperparameter of tree growth. Weight of the distance function for fitness calculation. Should be >= 0. Defaults to 1.0.
s_weight (float) – Hyperparameter of tree growth. Weight of the score function for fitness calculation. Should be >= 0. Defaults to 1.0.
a_weight (float) – Hyperparameter of tree growth. Weight of the accuracy function for fitness calculation. Should be >= 0. Defaults to 1.0.
pareto_fitness (bool) – Hyperparameter of tree growth. Determines , how the three fitness function components are treated. If
False, use scalarized fitness. IfTrue, use multi-criteria fitness. Defaults toFalse.score_func (callable or None) – Hyperparameter of tree growth. The score function maps from class probabilities in a leaf to a scalar, i.e.,
[py1, py2, ...] -> float. The result measures the concentration of the distribution and is used as the first fitness function component. If set toNone, useQTree.score_func_entropy. Defaults toNone.dist_func (callable or None) – Hyperparameter of tree growth. The distance function maps from class probabilities in two leaves to a scalar, i.e.,
[py1, py2, ..., py1', py2', ..] -> float. The result measures the distance between two distributions and is used as the second fitness function component. If set toNone, useQTree.dist_func_proba_dist_norm. Defaults toNone.acc_func (callable or None) – Hyperparameter of tree growth. The accuracy function maps from leaf to a scalar, i.e.,
[py1, py2, ...] -> float. The result measures the quality of the tree prediction and is used as the third fitness function component. If set toNone, useQTree.acc_func_acc_balanced. Defaults toNone.swap_func (callable or None) – Function (
qiskit.Circuit, q1, q2 -> None) that defines how a swap gate between two qubits with indicesq1andq2is realized. If set toNone, the callable is used, seequant.QTreeCircuitManager. Defaults toNone.mcswap_func (callable or None) – Function (
qiskit.Circuit, q1, q2, qc_list, mct_func -> None) that defines how a multi-controlled swap gate between two qubits with indicesq1andq2controlled by the qubits with indicesqc_listis realized. Also seemct_funcbelow. If set toNone, the callable is used, seequant.QTreeCircuitManager. Defaults toNone.mct_func (callable or None) – Function (
qiskit.Circuit, q1, q2, qc_list -> None) that defines how a multi-control Toffoli gate between two qubits with indicesq1andq2controlled by the qubits with indicesqc_listis realized. If set toNone, the callable is used, seequant.QTreeCircuitManager. Defaults toNone.pop_size (int) – Hyperparameter of tree growth. Population size used in the evolution algorithm. See
deapdocumentation for more details. Defults to 10.cx_pb (float) – Hyperparameter of tree growth. Crossover probability for
tools.cxOnePointused in the evolution algorithm. Seedeapdocumentation for more details. Defaults to 0.2.mut_pb (float) – Hyperparameter of tree growth. Mutation probability for
tools.mutUniformIntused in the evolution algorithm. Seedeapdocumentation for more details. Defaults to 0.2.mut_ind_pb (float) – Hyperparameter of tree growth. Ìndividual gene mutation probability for
tools.mutUniformIntused in the evolution algorithm. Seedeapdocumentation for more details. Defaults to 0.2.select_tournsize (int) – Hyperparameter of tree growth. Selection tournament size for
tools.selTournamentwithk=pop_size-1used in the evolution algorithm. Seedeapdocumentation for more details. Defaults to 3.max_generations (int) – Hyperparameter of tree growth. Maximum number of generations used in the evolution algorithm. See
deapdocumentation for more details. Defaults to 10.reuse_G_calc (bool) – Hyperparameter of tree growth. Determines how tree evaluations are performed. If
True, reuse tree evaluations. IfFalse, repeat evaluations (e.g., to take noisy simulations and imperfect backends into account). Defaults toTrue.mean_fits (bool) – Hyperparameter of tree growth. Determines how multiple evaluation of the same tree should be treated. If
True, fitnesses of identical trees are averaged (where differences can occur due to noise). IfFalse, only the newest fitness is used. Defaults toTrue.random_state (int or None) – Hyperparameter of tree growth. Random seed. Can also be a
numpy.random.RandomState. If set toNone, an unspecified seed is used. Defaults toNone.evo_callback (callable or None) – Callback function
(current_generation, pop) -> Nonefor tree growth. Called every iteration. If set toNone, callback function is ignored. Defaults toNone.remove_inactive_qubits (bool) – Affects the circuit representation of the decision tree. If
True, inactive qubits are removed from the circuit. IfFalse, they are included. Defaults toTrue.eliminate_first_swap (bool) – Affects the circuit representation of the decision tree. If
True, the first swap gate is removed by changing the qubit ordering. IfFalse, the first swap gate is included. Defaults toFalse.simultaneous_circuit_evaluation (bool) – Determines how trees (i.e., quantum circuits) are evaluated for the evolution algorithm when growing a tree. If
True, all circuits of one iteration are evaluated simultaneously (i.e., in one job). IfFalse, all circuits of one iteration are evaluated sequentially (i.e., as multiple jobs). Defaults toFalse.include_id (bool) – Affects the circuit representation of the decision tree. If
True, identity gates are included. IfFalse, they are omitted. Defaults toFalse.block_form (bool) – Affects the circuit representation of the decision tree. If
True, tree layers are grouped and represented by custom gates. IfFalse, all base gates are used instead. Defaults toFalse.use_barriers (bool) – Affects the circuit representation of the decision tree. If
True, barriers are inserted between tree layers. IfFalse, no barriers are used. Defaults toFalse.logger (logging.Logger or None) – Logger for all output purposes. If
None, the default logger is used. Defaults toNone.
- property G¶
Raw tree structure.
- Type:
list
- property X¶
Training data, features.
- Type:
numpy.ndarray
- static acc_func_acc_balanced(y_pred, y_true)[source]¶
Accuracy function as argument for
acc_funcin__init__. See there for more details.
- circuit(measure_y=True, measure_all_x=False, return_meta=False, return_depth=False)[source]¶
Build circuit representation of the decision tree.
- cl_tree_prediction_proba(X, **kwargs)[source]¶
Make class probability prediction with classical tree representation.
- property ct¶
Classical tree representation.
- Type:
- static dist_func_proba_dist_norm(py1, py2)[source]¶
Distance function as argument for
dist_funcin__init__. See there for more details.
- fit(X, y)[source]¶
Fit classifier: grow the decision tree using an evolutionary algorithm.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Feature vectors of training data. Note: will be converted into a binary array!
y (array-like of shape (n_samples, n_labels)) – Target labels of training data. Note: will be converted into a binary array!
- Returns:
self
- Return type:
returns an instance of self.
- grow(d_weight, s_weight, a_weight, pareto_fitness, max_depth, rng, pop_size, cx_pb, mut_pb, mut_ind_pb, select_tournsize, max_generations, reuse_G_calc, remove_inactive_qubits, eliminate_first_swap, include_id, block_form, use_barriers, simultaneous_circuit_evaluation, mean_fits)[source]¶
Grow decision tree. (Advanced functionality.)
- property path_result_dict¶
Tree traversal path summary.
- Type:
dict
- predict(X, **kwargs)[source]¶
Make class predictions using a classical representation of the decision tree.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Feature vectors of training data. Note: will be converted into a binary array!
**kwargs – Additional arguments for the prediction. Will be passed to the classical tree.
- Returns:
y – Predicted classes as a binary array.
- Return type:
ndarray of shape (n_samples, n_labels)
- predict_proba(X, **kwargs)[source]¶
Make class probability predictions using a classical representation of the decision tree.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Feature vectors of training data. Note: will be converted into a binary array!
**kwargs – Additional arguments for the prediction. Will be passed to the classical tree.
- Returns:
p – Predicted class probabilities.
- Return type:
ndarray of shape (n_samples, 2, n_labels)
- predict_query(query_probabilities)[source]¶
Make class probability predictions using a probabilistic quantum circuit query.
- Parameters:
query_probabilities (dict) – Quantum query of the form
{ key1 : probability1, key2 : probability2, ...}, wherekeyis a bitstring of inputs/features andprobabilitythe corresponding probability. The sum of all probabilities has to be normalized. To predict a single key, useprobability1 = 1.0.- Returns:
py – Predicted class probabilities as a binary array.
- Return type:
ndarray of shape (n_classes,)
- property qt¶
Circuit representation.
- Type:
qiskit.Circuit
- query_prediction(query_probabilities, path_result_dict=None, eliminate_first_swap=False, include_id=False, block_form=False, remove_inactive_qubits=False, return_additional_information=False)[source]¶
Make class predictions using a probabilistic quantum circuit query.
- static score_func_entropy(py)[source]¶
Score function as argument for
score_funcin__init__. See there for more details.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QTree¶
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object
- to_tree_dict(remove_inactive_qubits=False, eliminate_first_swap=False, include_id=False, block_form=False, use_barriers=False)[source]¶
Build tree dict. (Advanced functionality.)
- property tree_dict¶
Tree structure as dictionary containing the class probabilities of each leaf.
- Type:
dict
- property tree_dict_detailed¶
Tree structure as dictionary similar to
tree_dictwith more detailed information.- Type:
dict
- property y¶
Training data, labels.
- Type:
numpy.ndarray
- class qtree.qtree.QTreeBase(max_depth, quantum_backend, d_weight, s_weight, a_weight, pareto_fitness, score_func, dist_func, acc_func, swap_func, mcswap_func, mct_func, pop_size, cx_pb, mut_pb, mut_ind_pb, select_tournsize, max_generations, reuse_G_calc, mean_fits, random_state, evo_callback, remove_inactive_qubits, eliminate_first_swap, simultaneous_circuit_evaluation, include_id, block_form, use_barriers, logger)[source]¶
Bases:
BaseEstimator,ClassifierMixinBase class for quantum decision classifiers.
- Parameters:
max_depth (int) – Maximum depth of the decision tree. A tree of depth 0 consists only of the root.
quantum_backend (qiskit.providers.Backend) – Qiskit quantum backend used for all calculations. Can be either a simulator or an actual backend.
d_weight (float) – Hyperparameter of tree growth. Weight of the distance function for fitness calculation. Should be >= 0.
s_weight (float) – Hyperparameter of tree growth. Weight of the score function for fitness calculation. Should be >= 0.
a_weight (float) – Hyperparameter of tree growth. Weight of the accuracy function for fitness calculation. Should be >= 0.
pareto_fitness (bool) – Hyperparameter of tree growth. Determines , how the three fitness function components are treated. If
False, use scalarized fitness. IfTrue, use multi-criteria fitness.score_func (callable) – Hyperparameter of tree growth. The score function maps from class probabilities in a leaf to a scalar, i.e.,
[py1, py2, ...] -> float. The result measures the concentration of the distribution and is used as the first fitness function component.dist_func (callable) – Hyperparameter of tree growth. The distance function maps from class probabilities in two leaves to a scalar, i.e.,
[py1, py2, ..., py1', py2', ..] -> float. The result measures the distance between two distributions and is used as the second fitness function component.acc_func (callable) – Hyperparameter of tree growth. The accuracy function maps from leaf to a scalar, i.e.,
[py1, py2, ...] -> float. The result measures the quality of the tree prediction and is used as the third fitness function component.swap_func (callable or None) – Function (
qiskit.Circuit, q1, q2 -> None) that defines how a swap gate between two qubits with indicesq1andq2is realized. If set toNone, the callable is used, seequant.QTreeCircuitManager.mcswap_func (callable or None) – Function (
qiskit.Circuit, q1, q2, qc_list, mct_func -> None) that defines how a multi-controlled swap gate between two qubits with indicesq1andq2controlled by the qubits with indicesqc_listis realized. Also seemct_funcbelow. If set toNone, the callable is used, seequant.QTreeCircuitManager.mct_func (callable or None) – Function (
qiskit.Circuit, q1, q2, qc_list -> None) that defines how a multi-control Toffoli gate between two qubits with indicesq1andq2controlled by the qubits with indicesqc_listis realized. If set toNone, the callable is used, seequant.QTreeCircuitManager.pop_size (int) – Hyperparameter of tree growth. Population size used in the evolution algorithm. See
deapdocumentation for more details.cx_pb (float) – Hyperparameter of tree growth. Crossover probability for
tools.cxOnePointused in the evolution algorithm. Seedeapdocumentation for more details.mut_pb (float) – Hyperparameter of tree growth. Mutation probability for
tools.mutUniformIntused in the evolution algorithm. Seedeapdocumentation for more details.mut_ind_pb (float) – Hyperparameter of tree growth. Ìndividual gene mutation probability for
tools.mutUniformIntused in the evolution algorithm. Seedeapdocumentation for more details.select_tournsize (int) – Hyperparameter of tree growth. Selection tournament size for
tools.selTournamentwithk=pop_size-1used in the evolution algorithm. Seedeapdocumentation for more details.max_generations (int) – Hyperparameter of tree growth. Maximum number of generations used in the evolution algorithm. See
deapdocumentation for more details.reuse_G_calc (bool) – Hyperparameter of tree growth. Determines how tree evaluations are performed. If
True, reuse tree evaluations. IfFalse, repeat evaluations (e.g., to take noisy simulations and imperfect backends into account).mean_fits (bool) – Hyperparameter of tree growth. Determines how multiple evaluation of the same tree should be treated. If
True, fitnesses of identical trees are averaged (where differences can occur due to noise). IfFalse, only the newest fitness is used.random_state (int or None) – Hyperparameter of tree growth. Random seed. Can also be a
numpy.random.RandomState. If set toNone, an unspecified seed is used.evo_callback (callable or None) – Callback function
(current_generation, pop) -> Nonefor tree growth. Called every iteration. If set toNone, callback function is ignored.remove_inactive_qubits (bool) – Affects the circuit representation of the decision tree. If
True, inactive qubits are removed from the circuit. IfFalse, they are included.eliminate_first_swap (bool) – Affects the circuit representation of the decision tree. If
True, the first swap gate is removed by changing the qubit ordering. IfFalse, the first swap gate is included.simultaneous_circuit_evaluation (bool) – Determines how trees (i.e., quantum circuits) are evaluated for the evolution algorithm when growing a tree. If
True, all circuits of one iteration are evaluated simultaneously (i.e., in one job). IfFalse, all circuits of one iteration are evaluated sequentially (i.e., as multiple jobs).include_id (bool) – Affects the circuit representation of the decision tree. If
True, identity gates are included. IfFalse, they are omitted.block_form (bool) – Affects the circuit representation of the decision tree. If
True, tree layers are grouped and represented by custom gates. IfFalse, all base gates are used instead.use_barriers (bool) – Affects the circuit representation of the decision tree. If
True, barriers are inserted between tree layers. IfFalse, no barriers are used.logger (logging.Logger or None) – Logger for all output purposes. If
None, the default logger is used.
- static check_query_probabilities(query_probabilities, Nx)[source]¶
Check probabilistic queries (in form of dictionaries) for predictions.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QTreeBase¶
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object
qtree.ctree¶
Classical decision trees.
- class qtree.ctree.ClassicalTree(tree_dict, y_dim=None, y=None, tree_dict_detailed={}, logger=None)[source]¶
Bases:
ClassicalTreeBaseClassical decision tree base.
- Parameters:
tree_dict (dict) – Tree structure as dictionary, see
qtree.QTree.y_dim (int or None) – Number of binary labels. If None, extract from
tree_dict. Defaults to None.y (array-like of shape (n_samples, n_labels) or None) – Target labels of training data. Used to find majority classes for default predictions. If None, no defaults are used. Defaults to None.
tree_dict_detailed (dict or None) – Tree structure as detailed dictionary, see
qtree.QTree. Is used to add optional information to each node. Set to None to disable. Defaults to empty dictionary.logger (logging.Logger or None) – Logger for all output purposes. If None, print to std is used. Defaults to None.
- calc_difference_to_exact(paths, tree_dict_exact, tree_dict_exact_data_probabilities={}, N=None, epsilon=None, omit_warnings=False)[source]¶
Calculate the distance between the class probabilities in the leaves between the tree dictionary and another tree dictionary.
- calc_exact_probabilities(X, y, omit_warnings=False)[source]¶
Calculate the exact class prediction probabilities based on the data
Xandy.
- class qtree.ctree.ClassicalTreeBase(tree_dict, y_dim, y, tree_dict_detailed, logger)[source]¶
Bases:
NodeClassical decision tree base.
- Parameters:
tree_dict (dict) – Tree structure as dictionary, see
qtree.QTree.y_dim (int or None) – Number of binary labels. If None, extract from
tree_dict.y (array-like of shape (n_samples, n_labels) or None) – Target labels of training data. Used to find majority classes for default predictions. If None, no defaults are used.
tree_dict_detailed (dict or None) – Tree structure as detailed dictionary, see
qtree.QTree. Is used to add optional information to each node. Set to None to disable.logger (logging.Logger or None) – Logger for all output purposes. If None, print to std is used.
- class qtree.ctree.Node(depth, y_dim, default_y=None, logger=None)[source]¶
Bases:
objectClassical decision tree node (root, inner node or leaf).
- Parameters:
depth (int) – Depth of this node. The root corresponds to
depth=0.y_dim (int) – Number of binary labels.
default_y (int or None) – Default value based on majority of classes in training data. Is used if a classification is ambiguous in a leaf. Set to None to disable.
logger (logging.Logger or None) – Logger for all output purposes. If None, print to std is used.
qtree.quant¶
Quantum circuit realization for quantum decision trees.
- class qtree.quant.QTreeCircuitManager(swap_func=None, mcswap_func=None, mct_func=None, logger=None)[source]¶
Bases:
objectQuantum circuit manager for quantum decision trees.
- Parameters:
swap_func (callable or None) – Function (
qiskit.Circuit, q1, q2 -> None) that defines how a swap gate between two qubits with indicesq1andq2is realized. If set to None,QTreeCircuitManager.gateop_swap_defaultis used. Defaults toNone.mcswap_func (callable or None) – Function (
qiskit.Circuit, q1, q2, qc_list, mct_func -> None) that defines how a multi-controlled swap gate between two qubits with indicesq1andq2controlled by the qubits with indicesqc_listis realized. Also seemct_funcbelow. If set to None,QTreeCircuitManager.gateop_mcswap_uis used. Defaults toNone.mct_func (callable or None) – Function (
qiskit.Circuit, q1, q2, qc_list -> None) that defines how a multi-control Toffoli gate between two qubits with indicesq1andq2controlled by the qubits with indicesqc_listis realized. If set to None,QTreeCircuitManager.gateop_mct_defaultis used. Defaults toNone.logger (logging.Logger or None) – Logger for all output purposes. If
None, the default logger is used. Defaults toNone.
- build_circuit(G, Nx, Ny, init, measure_y=True, measure_all_x=False, remove_inactive_qubits=False, eliminate_first_swap=False, include_id=False, block_form=False, use_barriers=False)[source]¶
Build circuit representation of decision tree.
- static gateop_mcswap_u(circ, q1, q2, qc_list, mct_func)[source]¶
MCSWAP function: use custom unitary representation. Used as argument for
mcswap_funcin__init__. See there for more details.
- static gateop_mcswap_xmctx(circ, q1, q2, qc_list, mct_func)[source]¶
MCSWAP function: use cnot mcswap representation. Used as argument for
mcswap_funcin__init__. See there for more details.
- static gateop_mct_default(circ, q1, q2, qc_list)[source]¶
MCT function: use default mct representation. Used as argument for
mct_funcin__init__. See there for more details.
- static gateop_swap_cnot(circ, q1, q2)[source]¶
SWAP function: use cnot swap representation. Used as argument for
swap_funcin__init__. See there for more details.
qtree.data¶
Data for quantum decision trees.
- class qtree.data.BDS(**kwargs)[source]¶
Bases:
ABCAbstract base class for binary data sets.
- Parameters:
**kwargs (Arguments for data loader.) –
- property X¶
Features.
- Type:
numpy.ndarray of shape (n_samples,n_features)
- property y¶
Labels.
- Type:
numpy.ndarray of shape (n_samples,n_labels)
- class qtree.data.BDS_test(**kwargs)[source]¶
Bases:
BDSBinary data set: XOR test data set consisting of 7 features and 1 label.
- Parameters:
**kwargs –
N(int) determines the number of data points, defaults to 5.
- class qtree.data.BDS_tic_tac_toe(root='', **kwargs)[source]¶
Bases:
BDSBinary data set: Binarized version of the Tic-Tac-Toe Endgame Data Set. Each 15-dimensional feature vector represents a Tic-Tac-Toe playfield, the corresponding label represents the winning player.
Original source: https://archive.ics.uci.edu/ml/datasets/Tic-Tac-Toe+Endgame
Original creator: David W. Aha (aha@cs.jhu.edu)
Note: Encoding to binary and decoding from binary is realized via
BDS_tic_tac_toe._encode_xandBDS_tic_tac_toe._decode_x, respectively.- Parameters:
root (str) – Root directory to load data file
data/tictactoe/tic-tac-toe.npz.**kwargs – Unused.