skqulacs.circuit.circuit module#
- class skqulacs.circuit.circuit.LearningCircuit(n_qubit: int)[source]#
Bases:
object
Construct and run quantum circuit for QNN.
## About parameters
This class manages parameters of underlying ParametricQuantumCircuit. A parameter has either type of features: learning and input.
Learning parameter represents a parameter to be optimized. This is updated by LearningCircuit.update_parameter().
Input parameter represents a placeholder of circuit input. This is updated in a execution of LearningCircuit.run() while applying func of the parameter.
And there is a parameter being both learning and input one. This parameter transforms its input by applying the parameter’s func with its learning parameter.
## Execution flow
Set up gates by LearningCircuit.add_*_gate().
For each execution, at first, feed input parameter with the value computed from input data x.
Apply |0> state to the circuit.
Compute optimized learning parameters in a certain way.
Update the learning parameters in the circuit with the optimized ones by LearningCircuit.update_parameters().
- Parameters
n_qubit – The number of qubits in the circuit.
Examples
>>> from skqulacs.circuit import LearningCircuit >>> from skqulacs.qnn.regressor import QNNRegressor >>> n_qubit = 2 >>> circuit = LearningCircuit(n_qubit) >>> theta = circuit.add_parametric_RX_gate(0, 0.5) >>> circuit.add_parametric_RY_gate(1, 0.1, share_with=theta) >>> circuit.add_input_RZ_gate(1, np.arcsin) >>> model = QNNRegressor(circuit) >>> _, theta = model.fit(x_train, y_train, maxiter=1000) >>> x_list = np.arange(x_min, x_max, 0.02) >>> y_pred = qnn.predict(theta, x_list)
- add_CNOT_gate(control_index: int, target_index: int) None [source]#
- Parameters
control_index – Index of control qubit.
target_index – Index of target qubit.
- add_RX_gate(index: int, angle: float) None [source]#
- Parameters
index – Index of qubit to add RX gate.
angle – Rotation angle.
- add_RY_gate(index: int, parameter: float) None [source]#
- Parameters
index – Index of qubit to add RY gate.
angle – Rotation angle.
- add_RZ_gate(index: int, parameter: float) None [source]#
- Parameters
index – Index of qubit to add RZ gate.
angle – Rotation angle.
- add_gate(gate: qulacs_core.QuantumGateBase) None [source]#
Add arbitrary gate.
- Parameters
gate – Gate to add.
- add_input_RX_gate(index: int, input_func: typing.Callable[[numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]], float] = <function LearningCircuit.<lambda>>) None [source]#
- Parameters
index – Index of qubit to add RX gate.
input_func – Function transforming input value.
- add_input_RY_gate(index: int, input_func: typing.Callable[[numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]], float] = <function LearningCircuit.<lambda>>) None [source]#
- Parameters
index – Index of qubit to add RY gate.
input_func – Function transforming input value.
- add_input_RZ_gate(index: int, input_func: typing.Callable[[numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]], float] = <function LearningCircuit.<lambda>>) None [source]#
- Parameters
index – Index of qubit to add RZ gate.
input_func – Function transforming input value.
- add_parametric_RX_gate(index: int, parameter: float, share_with: Optional[int] = None, share_with_coef: Optional[float] = None) int [source]#
- Parameters
index – Index of qubit to add RX gate.
parameter – Initial parameter of this gate.
share_with – parameter_id to share the parameter in ParametricQuantumCircuit.
share_with_coef – Coefficients for shared parameters which is share_with. if ‘share_with’ is none, share_with_coef is skiped.
- Returns
parameter_id which is added or updated.
- add_parametric_RY_gate(index: int, parameter: float, share_with: Optional[int] = None, share_with_coef: Optional[float] = None) int [source]#
- Parameters
index – Index of qubit to add RY gate.
parameter – Initial parameter of this gate.
share_with – parameter_id to share the parameter in ParametricQuantumCircuit.
share_with_coef – Coefficients for shared parameters which is share_with.
- Returns
parameter_id which is added or updated.
- add_parametric_RZ_gate(index: int, parameter: float, share_with: Optional[int] = None, share_with_coef: Optional[float] = None) int [source]#
- Parameters
index – Index of qubit to add RZ gate.
parameter – Initial parameter of this gate.
share_with – parameter_id to share the parameter in ParametricQuantumCircuit.
share_with_coef – Coefficients for shared parameters which is share_with. if ‘share_with’ is none, share_with_coef is skiped.
- Returns
parameter_id which is added or updated.
- add_parametric_input_RX_gate(index: int, parameter: float, input_func: typing.Callable[[float, numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]], float] = <function LearningCircuit.<lambda>>) None [source]#
- Parameters
index – Index of qubit to add RX gate.
parameter – Initial parameter of this gate.
input_func – Function transforming this gate’s parameter and input value.
- add_parametric_input_RY_gate(index: int, parameter: float, input_func: typing.Callable[[float, numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]], float] = <function LearningCircuit.<lambda>>) None [source]#
- Parameters
index – Index of qubit to add RY gate.
parameter – Initial parameter of this gate.
input_func – Function transforming this gate’s parameter and input value.
- add_parametric_input_RZ_gate(index: int, parameter: float, input_func: typing.Callable[[float, numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]], float] = <function LearningCircuit.<lambda>>) None [source]#
- Parameters
index – Index of qubit to add RZ gate.
parameter – Initial parameter of this gate.
input_func – Function transforming this gate’s parameter and input value.
- add_parametric_multi_Pauli_rotation_gate(target: List[int], pauli_id: List[int], initial_angle: float) None [source]#
- backprop(self, x: List[float], obs) List[Float] [source]#
xは入力の状態で、yは出力値の微分値 帰ってくるのは、それぞれのパラメータに関する微分値 例えば、出力が[0,2] だったらパラメータの1項目は期待する出力に関係しない、2項目をa上げると回路の出力は2a上がる?
-> c++のParametricQuantumCircuitクラスを呼び出す backprop(GeneralQuantumOperator* obs)
->うまくやってbackpropする。 現実だと不可能な演算も含むが、気にしない
- backprop_inner_product(x: List[float], state: qulacs_core.QuantumState) List[float] [source]#
backprop(self, x: List[float], state)->List[Float]
inner_productでbackpropします。
- n_qubit: int#
- run(x: List[float] = []) qulacs_core.QuantumState [source]#
Determine parameters for input gate based on x and apply the circuit to |0> state.
- Parameters
x – Input data whose shape is (n_features,).
- Returns
Quantum state applied the circuit.