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

  1. Set up gates by LearningCircuit.add_*_gate().

  2. For each execution, at first, feed input parameter with the value computed from input data x.

  3. Apply |0> state to the circuit.

  4. Compute optimized learning parameters in a certain way.

  5. 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_H_gate(index: int) None[source]#
Parameters

index – Index of qubit to put H gate.

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_X_gate(index: int) None[source]#
Parameters

index – Index of qubit to add X gate.

add_Y_gate(index: int) None[source]#
Parameters

index – Index of qubit to add Y gate.

add_Z_gate(index: int) None[source]#
Parameters

index – Index of qubit to add Z gate.

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します。

get_circuit_depth() int[source]#
get_circuit_info() qulacs_core.ParametricQuantumCircuit[source]#
get_parameters() List[float][source]#

Get a list of learning parameters’ values.

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.

run_x_no_change() qulacs_core.QuantumState[source]#

Run the circuit while x is not changed from the previous run. (can change parameters)

update_parameters(theta: List[float]) None[source]#

Update learning parameter of the circuit with given theta.

Parameters

theta – New learning parameters.