def test_opflow_qnn_2_2(self, config): """Test Opflow QNN with input/output dimension 2/2.""" q_i, input_grad_required = config if q_i == STATEVECTOR: quantum_instance = self.sv_quantum_instance elif q_i == QASM: quantum_instance = self.qasm_quantum_instance else: quantum_instance = None # construct parametrized circuit params_1 = [Parameter("input1"), Parameter("weight1")] qc_1 = QuantumCircuit(1) qc_1.h(0) qc_1.ry(params_1[0], 0) qc_1.rx(params_1[1], 0) qc_sfn_1 = StateFn(qc_1) # construct cost operator h_1 = StateFn(PauliSumOp.from_list([("Z", 1.0), ("X", 1.0)])) # combine operator and circuit to objective function op_1 = ~h_1 @ qc_sfn_1 # construct parametrized circuit params_2 = [Parameter("input2"), Parameter("weight2")] qc_2 = QuantumCircuit(1) qc_2.h(0) qc_2.ry(params_2[0], 0) qc_2.rx(params_2[1], 0) qc_sfn_2 = StateFn(qc_2) # construct cost operator h_2 = StateFn(PauliSumOp.from_list([("Z", 1.0), ("X", 1.0)])) # combine operator and circuit to objective function op_2 = ~h_2 @ qc_sfn_2 op = ListOp([op_1, op_2]) qnn = OpflowQNN( op, [params_1[0], params_2[0]], [params_1[1], params_2[1]], quantum_instance=quantum_instance, ) qnn.input_gradients = input_grad_required test_data = [np.array([1, 2]), np.array([[1, 2], [3, 4]])] # test model self.validate_output_shape(qnn, test_data) # test the qnn after we set a quantum instance if quantum_instance is None: qnn.quantum_instance = self.qasm_quantum_instance self.validate_output_shape(qnn, test_data)
def test_opflow_qnn_2_1(self, config): """Test Opflow QNN with input/output dimension 2/1.""" q_i, input_grad_required = config # construct QNN if q_i == STATEVECTOR: quantum_instance = self.sv_quantum_instance elif q_i == QASM: quantum_instance = self.qasm_quantum_instance else: quantum_instance = None # specify how to evaluate expected values and gradients expval = PauliExpectation() gradient = Gradient() # construct parametrized circuit params = [ Parameter("input1"), Parameter("input2"), Parameter("weight1"), Parameter("weight2"), ] qc = QuantumCircuit(2) qc.h(0) qc.ry(params[0], 0) qc.ry(params[1], 1) qc.rx(params[2], 0) qc.rx(params[3], 1) qc_sfn = StateFn(qc) # construct cost operator cost_operator = StateFn(PauliSumOp.from_list([("ZZ", 1.0), ("XX", 1.0)])) # combine operator and circuit to objective function op = ~cost_operator @ qc_sfn # define QNN qnn = OpflowQNN( op, params[:2], params[2:], expval, gradient, quantum_instance=quantum_instance, ) qnn.input_gradients = input_grad_required test_data = [np.array([1, 2]), np.array([[1, 2]]), np.array([[1, 2], [3, 4]])] # test model self.validate_output_shape(qnn, test_data) # test the qnn after we set a quantum instance if quantum_instance is None: qnn.quantum_instance = self.qasm_quantum_instance self.validate_output_shape(qnn, test_data)