def test_parameterized_qobj(self): """grouped pauli expectation test""" two_qubit_h2 = ( (-1.052373245772859 * I ^ I) + (0.39793742484318045 * I ^ Z) + (-0.39793742484318045 * Z ^ I) + (-0.01128010425623538 * Z ^ Z) + (0.18093119978423156 * X ^ X) ) aer_sampler = CircuitSampler( self.sampler.quantum_instance, param_qobj=True, attach_results=True ) ansatz = RealAmplitudes() ansatz.num_qubits = 2 observable_meas = self.expect.convert(StateFn(two_qubit_h2, is_measurement=True)) ansatz_circuit_op = CircuitStateFn(ansatz) expect_op = observable_meas.compose(ansatz_circuit_op).reduce() def generate_parameters(num): param_bindings = {} for param in ansatz.parameters: values = [] for _ in range(num): values.append(np.random.rand()) param_bindings[param] = values return param_bindings def validate_sampler(ideal, sut, param_bindings): expect_sampled = ideal.convert(expect_op, params=param_bindings).eval() actual_sampled = sut.convert(expect_op, params=param_bindings).eval() self.assertTrue( np.allclose(actual_sampled, expect_sampled), "%s != %s" % (actual_sampled, expect_sampled), ) def get_circuit_templates(sampler): return sampler._transpiled_circ_templates def validate_aer_binding_used(templates): self.assertIsNotNone(templates) def validate_aer_templates_reused(prev_templates, cur_templates): self.assertIs(prev_templates, cur_templates) validate_sampler(self.sampler, aer_sampler, generate_parameters(1)) cur_templates = get_circuit_templates(aer_sampler) validate_aer_binding_used(cur_templates) prev_templates = cur_templates validate_sampler(self.sampler, aer_sampler, generate_parameters(2)) cur_templates = get_circuit_templates(aer_sampler) validate_aer_templates_reused(prev_templates, cur_templates) prev_templates = cur_templates validate_sampler(self.sampler, aer_sampler, generate_parameters(2)) # same num of params cur_templates = get_circuit_templates(aer_sampler) validate_aer_templates_reused(prev_templates, cur_templates)
def test_parameterized_qobj(self): """ grouped pauli expectation test """ two_qubit_h2 = (-1.052373245772859 * I ^ I) + \ (0.39793742484318045 * I ^ Z) + \ (-0.39793742484318045 * Z ^ I) + \ (-0.01128010425623538 * Z ^ Z) + \ (0.18093119978423156 * X ^ X) aer_sampler = CircuitSampler(self.sampler.quantum_instance, param_qobj=True, attach_results=True) var_form = RealAmplitudes() var_form.num_qubits = 2 observable_meas = self.expect.convert(StateFn(two_qubit_h2, is_measurement=True)) ansatz_circuit_op = CircuitStateFn(var_form) expect_op = observable_meas.compose(ansatz_circuit_op).reduce() def generate_parameters(num): param_bindings = {} for param in var_form.parameters: values = [] for _ in range(num): values.append(np.random.rand()) param_bindings[param] = values return param_bindings def validate_sampler(ideal, sut, param_bindings): expect_sampled = ideal.convert(expect_op, params=param_bindings).eval() actual_sampled = sut.convert(expect_op, params=param_bindings).eval() self.assertAlmostEqual(actual_sampled, expect_sampled, delta=.1) def get_binding_status(sampler): # sampler._transpiled_circ_templates: # set only if aer's binding was used. # renewed if var_form is renewed, # sampler._last_ready_circ: # set only if aer's binding was used. # renewed if var_form or # of parameter sets are changed return sampler._transpiled_circ_templates def validate_aer_binding_used(status): self.assertIsNotNone(status) def validate_aer_templates_reused(prev_status, cur_status): self.assertIs(prev_status, cur_status) validate_sampler(self.sampler, aer_sampler, generate_parameters(1)) cur_status = get_binding_status(aer_sampler) validate_aer_binding_used(cur_status) prev_status = cur_status validate_sampler(self.sampler, aer_sampler, generate_parameters(2)) cur_status = get_binding_status(aer_sampler) validate_aer_templates_reused(prev_status, cur_status) prev_status = cur_status validate_sampler(self.sampler, aer_sampler, generate_parameters(2)) # same num of params cur_status = get_binding_status(aer_sampler) validate_aer_templates_reused(prev_status, cur_status)