def test_circuit_state_fn_from_dict_as_sum(self): """state fn circuit from dict as sum test """ statedict = {'1010101': .5, '1000000': .1, '0000000': .2j, '1111111': 0.5j} sfc_sum = CircuitStateFn.from_dict(statedict) self.assertIsInstance(sfc_sum, SummedOp) for sfc_op in sfc_sum.oplist: self.assertIsInstance(sfc_op, CircuitStateFn) samples = sfc_op.sample() self.assertIn(list(samples.keys())[0], statedict) self.assertEqual(sfc_op.coeff, statedict[list(samples.keys())[0]]) np.testing.assert_array_almost_equal(StateFn(statedict).to_matrix(), sfc_sum.to_matrix())
def test_sampling(self): """ state fn circuit from dict initialize test """ statedict = {'101': .5 + 1.j, '100': .1 + 2.j, '000': .2 + 0.j, '111': .5 + 1.j} sfc = CircuitStateFn.from_dict(statedict) circ_samples = sfc.sample() dict_samples = StateFn(statedict).sample() vec_samples = StateFn(statedict).to_matrix_op().sample() for k, v in circ_samples.items(): self.assertIn(k, dict_samples) self.assertIn(k, vec_samples) # It's ok if these are far apart because the dict is sampled. self.assertAlmostEqual(v, np.abs(dict_samples[k]) ** .5, delta=.5) self.assertAlmostEqual(v, np.abs(vec_samples[k]) ** .5, delta=.5)
def test_circuit_state_fn_from_dict_initialize(self): """state fn circuit from dict initialize test""" statedict = {"101": 0.5, "100": 0.1, "000": 0.2, "111": 0.5} sfc = CircuitStateFn.from_dict(statedict) self.assertIsInstance(sfc, CircuitStateFn) samples = sfc.sample() np.testing.assert_array_almost_equal( StateFn(statedict).to_matrix(), np.round(sfc.to_matrix(), decimals=1)) for k, v in samples.items(): self.assertIn(k, statedict) # It's ok if these are far apart because the dict is sampled. self.assertAlmostEqual(v, np.abs(statedict[k])**0.5, delta=0.5) # Follows same code path as above, but testing to be thorough sfc_vector = CircuitStateFn.from_vector(StateFn(statedict).to_matrix()) np.testing.assert_array_almost_equal( StateFn(statedict).to_matrix(), sfc_vector.to_matrix())