scikit_quri.circuit package#
Submodules#
scikit_quri.circuit.circuit module#
- class scikit_quri.circuit.circuit.LearningCircuit(n_qubits)[source]#
Bases:
objectParametric quantum circuit for quantum machine learning.
Manages three types of circuit parameters:
Fixed gates: Non-parametric gates (X, Y, Z, H, CNOT, etc.).
Input gates (
add_input_R*): Rotation angle is computed from input dataxvia a user-supplied function at inference time.Learnable gates (
add_parametric_R*): Rotation angle is a trainable parameter updated during optimization. Multiple gates can share a single parameter viashare_with/share_with_coef.Parametric-input gates (
add_parametric_input_R*): Angle depends on both a learnable parameter and input data (e.g.f(theta, x)).
- Parameters:
n_qubits (int) – Number of qubits in the circuit.
Example
>>> circuit = LearningCircuit(n_qubits=4) >>> circuit.add_input_RY_gate(0, lambda x: np.arcsin(x[0])) >>> param_id = circuit.add_parametric_RX_gate(1) >>> bound = circuit.bind_input_and_parameters(x, theta)
- circuit: ParametricQuantumCircuit#
- add_input_RX_gate(qubit, input_function)[source]#
Add an RX gate whose angle is determined by input data at inference time.
- add_input_RY_gate(qubit, input_function)[source]#
Add an RY gate whose angle is determined by input data at inference time.
- add_input_RZ_gate(qubit, input_function)[source]#
Add an RZ gate whose angle is determined by input data at inference time.
- add_parametric_RX_gate(qubit, share_with=None, share_with_coef=None)[source]#
Add a trainable RX gate and return its parameter ID.
- Parameters:
qubit (int) – Index of the target qubit.
share_with (Optional[int]) – If given, share the learnable parameter with the gate whose
parameter_idequalsshare_with.share_with_coef (Optional[float]) – Coefficient applied to the shared parameter value (angle = shared_value * coef). Used only when
share_withis set.
- Returns:
Index of the learnable parameter assigned to this gate.
- Return type:
parameter_id
- add_parametric_RY_gate(qubit, share_with=None, share_with_coef=None)[source]#
Add a trainable RY gate and return its parameter ID.
- add_parametric_RZ_gate(qubit, share_with=None, share_with_coef=None)[source]#
Add a trainable RZ gate and return its parameter ID.
- add_parametric_multi_Pauli_rotation_gate(targets, pauli_ids)[source]#
Add a trainable multi-qubit Pauli rotation gate.
Note: this gate is not integrated with the share_with mechanism.
- add_parametric_input_RX_gate(index, input_func=<function LearningCircuit.<lambda>>)[source]#
Add an RX gate whose angle depends on both a learnable parameter and input data.
- add_parametric_input_RY_gate(index, input_func=<function LearningCircuit.<lambda>>)[source]#
Add an RY gate whose angle depends on both a learnable parameter and input data.
- add_parametric_input_RZ_gate(index, input_func=<function LearningCircuit.<lambda>>)[source]#
Add an RZ gate whose angle depends on both a learnable parameter and input data.
- property parameter_count: int#
Total number of parametric slots in the underlying circuit (input + learnable).
- property learning_params_count: int#
Number of unique learnable parameters (i.e. the length of the theta vector).
- get_learning_params_indexes()[source]#
Circuit-level indices of all learnable parameter slots.
Positions belonging to the same parameter via share_with appear separately.
- get_minimum_learning_param_indexes()[source]#
One representative circuit-level index per unique learnable parameter.
- get_learning_param_grad_aggregators()[source]#
For each learning parameter, list
(gate_pos, coef)tuples that must be summed to compose the per-learning-param gradient from per-gate gradients.share_withproduces multiple entries per learning param.
- bind_input_and_parameters(x, parameters)[source]#
Bind input data and learnable parameters to produce a concrete circuit.
- generate_bound_params(x, parameters)[source]#
Compute the full gate-level parameter list from input data and learnable parameters.
- to_batched(data, parameters)[source]#
Build a batched parameter array for use with scaluq (quri-parts-scaluq).
- to_batched_for_gradient(data, parameters, delta=1e-05)[source]#
Build shifted parameter arrays for numerical gradient estimation.
For each sample and each learning parameter, creates two rows with the parameter shifted by +delta/2 and -delta/2.
- Row layout: for sample i and learning param j,
plus row = i * 2 * learning_params_count + 2 * j minus row = i * 2 * learning_params_count + 2 * j + 1
- backprop_inner_product(x, theta, state)[source]#
Compute gradients of learnable parameters via qulacs backpropagation using inner product.
Thin delegator to
scikit_quri.circuit.gradient.backprop_inner_product().- Parameters:
x (NDArray[np.float64]) –
theta (NDArray[np.float64]) –
state (QulacsQuantumState) –
- Return type:
NDArray[np.float64]
- backprop_innner_product(x, theta, state)#
Compute gradients of learnable parameters via qulacs backpropagation using inner product.
Thin delegator to
scikit_quri.circuit.gradient.backprop_inner_product().- Parameters:
x (NDArray[np.float64]) –
theta (NDArray[np.float64]) –
state (QulacsQuantumState) –
- Return type:
NDArray[np.float64]
- hadamard_gradient(x, theta, operator, estimator)[source]#
Compute gradients of learnable parameters via the Hadamard test.
Thin delegator to
scikit_quri.circuit.gradient.hadamard_gradient().- Parameters:
x (NDArray[np.float64]) –
theta (NDArray[np.float64]) –
operator (Operator) –
estimator (ConcurrentQuantumEstimator) –
- Return type:
NDArray[np.float64]
scikit_quri.circuit.pre_defined module#
- scikit_quri.circuit.pre_defined.CNOT()#
- scikit_quri.circuit.pre_defined.CZ()#
- scikit_quri.circuit.pre_defined.create_qcl_ansatz(n_qubit, c_depth, time_step=0.5, seed=0)[source]#
Create a circuit used in this page: https://dojo.qulacs.org/ja/latest/notebooks/5.2_Quantum_Circuit_Learning.html
- Parameters:
- Return type:
Examples
>>> n_qubit = 4 >>> circuit = create_qcl_ansatz(n_qubit, 3, 0.5) >>> qnn = QNNRegressor(circuit) >>> qnn.fit(x_train, y_train)
- scikit_quri.circuit.pre_defined.create_farhi_neven_ansatz(n_qubit, c_depth, seed=0)[source]#
- Parameters:
- Return type:
- scikit_quri.circuit.pre_defined.create_ibm_embedding_circuit(n_qubit)[source]#
Create circuit proposed in https://arxiv.org/abs/1802.06002.
- Parameters:
n_qubits – number of qubits
n_qubit (int) –
- Return type:
- scikit_quri.circuit.pre_defined.create_dqn_cl(n_qubit, c_depth, s_qubit)[source]#
- Parameters:
- Return type:
- scikit_quri.circuit.pre_defined.create_dqn_cl_no_cz(n_qubit, c_depth)[source]#
- Parameters:
- Return type:
- scikit_quri.circuit.pre_defined.create_qcnn_ansatz(n_qubit, seed=0)[source]#
Creates circuit used in https://www.tensorflow.org/quantum/tutorials/qcnn?hl=en, Section 1.
- Parameters:
- Return type:
Module contents#
- class scikit_quri.circuit.LearningCircuit(n_qubits)[source]
Bases:
objectParametric quantum circuit for quantum machine learning.
Manages three types of circuit parameters:
Fixed gates: Non-parametric gates (X, Y, Z, H, CNOT, etc.).
Input gates (
add_input_R*): Rotation angle is computed from input dataxvia a user-supplied function at inference time.Learnable gates (
add_parametric_R*): Rotation angle is a trainable parameter updated during optimization. Multiple gates can share a single parameter viashare_with/share_with_coef.Parametric-input gates (
add_parametric_input_R*): Angle depends on both a learnable parameter and input data (e.g.f(theta, x)).
- Parameters:
n_qubits (int) – Number of qubits in the circuit.
Example
>>> circuit = LearningCircuit(n_qubits=4) >>> circuit.add_input_RY_gate(0, lambda x: np.arcsin(x[0])) >>> param_id = circuit.add_parametric_RX_gate(1) >>> bound = circuit.bind_input_and_parameters(x, theta)
- n_qubits: int
- circuit: ParametricQuantumCircuit
- add_gate(gate)[source]
Add arbitrary gate.
- Parameters:
gate (QuantumGate) –
- Return type:
None
- add_CNOT_gate(control_index, target_index)[source]
- add_input_RX_gate(qubit, input_function)[source]
Add an RX gate whose angle is determined by input data at inference time.
- add_input_RY_gate(qubit, input_function)[source]
Add an RY gate whose angle is determined by input data at inference time.
- add_input_RZ_gate(qubit, input_function)[source]
Add an RZ gate whose angle is determined by input data at inference time.
- add_parametric_RX_gate(qubit, share_with=None, share_with_coef=None)[source]
Add a trainable RX gate and return its parameter ID.
- Parameters:
qubit (int) – Index of the target qubit.
share_with (Optional[int]) – If given, share the learnable parameter with the gate whose
parameter_idequalsshare_with.share_with_coef (Optional[float]) – Coefficient applied to the shared parameter value (angle = shared_value * coef). Used only when
share_withis set.
- Returns:
Index of the learnable parameter assigned to this gate.
- Return type:
parameter_id
- add_parametric_RY_gate(qubit, share_with=None, share_with_coef=None)[source]
Add a trainable RY gate and return its parameter ID.
- add_parametric_RZ_gate(qubit, share_with=None, share_with_coef=None)[source]
Add a trainable RZ gate and return its parameter ID.
- add_parametric_multi_Pauli_rotation_gate(targets, pauli_ids)[source]
Add a trainable multi-qubit Pauli rotation gate.
Note: this gate is not integrated with the share_with mechanism.
- add_parametric_input_RX_gate(index, input_func=<function LearningCircuit.<lambda>>)[source]
Add an RX gate whose angle depends on both a learnable parameter and input data.
- add_parametric_input_RY_gate(index, input_func=<function LearningCircuit.<lambda>>)[source]
Add an RY gate whose angle depends on both a learnable parameter and input data.
- add_parametric_input_RZ_gate(index, input_func=<function LearningCircuit.<lambda>>)[source]
Add an RZ gate whose angle depends on both a learnable parameter and input data.
- property parameter_count: int
Total number of parametric slots in the underlying circuit (input + learnable).
- property input_params_count: int
Number of input-data-driven parameters.
- property learning_params_count: int
Number of unique learnable parameters (i.e. the length of the theta vector).
- get_learning_params_indexes()[source]
Circuit-level indices of all learnable parameter slots.
Positions belonging to the same parameter via share_with appear separately.
- get_minimum_learning_param_indexes()[source]
One representative circuit-level index per unique learnable parameter.
- get_input_params_indexes()[source]
Circuit-level indices of all input-data-driven parameter slots.
- get_learning_param_grad_aggregators()[source]
For each learning parameter, list
(gate_pos, coef)tuples that must be summed to compose the per-learning-param gradient from per-gate gradients.share_withproduces multiple entries per learning param.
- bind_input_and_parameters(x, parameters)[source]
Bind input data and learnable parameters to produce a concrete circuit.
- generate_bound_params(x, parameters)[source]
Compute the full gate-level parameter list from input data and learnable parameters.
- to_batched(data, parameters)[source]
Build a batched parameter array for use with scaluq (quri-parts-scaluq).
- to_batched_for_gradient(data, parameters, delta=1e-05)[source]
Build shifted parameter arrays for numerical gradient estimation.
For each sample and each learning parameter, creates two rows with the parameter shifted by +delta/2 and -delta/2.
- Row layout: for sample i and learning param j,
plus row = i * 2 * learning_params_count + 2 * j minus row = i * 2 * learning_params_count + 2 * j + 1
- backprop_inner_product(x, theta, state)[source]
Compute gradients of learnable parameters via qulacs backpropagation using inner product.
Thin delegator to
scikit_quri.circuit.gradient.backprop_inner_product().- Parameters:
x (NDArray[np.float64]) –
theta (NDArray[np.float64]) –
state (QulacsQuantumState) –
- Return type:
NDArray[np.float64]
- backprop_innner_product(x, theta, state)
Compute gradients of learnable parameters via qulacs backpropagation using inner product.
Thin delegator to
scikit_quri.circuit.gradient.backprop_inner_product().- Parameters:
x (NDArray[np.float64]) –
theta (NDArray[np.float64]) –
state (QulacsQuantumState) –
- Return type:
NDArray[np.float64]
- hadamard_gradient(x, theta, operator, estimator)[source]
Compute gradients of learnable parameters via the Hadamard test.
Thin delegator to
scikit_quri.circuit.gradient.hadamard_gradient().- Parameters:
x (NDArray[np.float64]) –
theta (NDArray[np.float64]) –
operator (Operator) –
estimator (ConcurrentQuantumEstimator) –
- Return type:
NDArray[np.float64]
- scikit_quri.circuit.create_qcl_ansatz(n_qubit, c_depth, time_step=0.5, seed=0)[source]
Create a circuit used in this page: https://dojo.qulacs.org/ja/latest/notebooks/5.2_Quantum_Circuit_Learning.html
- Parameters:
- Return type:
Examples
>>> n_qubit = 4 >>> circuit = create_qcl_ansatz(n_qubit, 3, 0.5) >>> qnn = QNNRegressor(circuit) >>> qnn.fit(x_train, y_train)
- scikit_quri.circuit.create_farhi_neven_ansatz(n_qubit, c_depth, seed=0)[source]
- Parameters:
- Return type:
- scikit_quri.circuit.create_ibm_embedding_circuit(n_qubit)[source]
Create circuit proposed in https://arxiv.org/abs/1802.06002.
- Parameters:
n_qubits – number of qubits
n_qubit (int) –
- Return type: