def test_circuit_state_fn_from_dict_initialize(self): """ state fn circuit from dict initialize test """ statedict = {'101': .5, '100': .1, '000': .2, '111': .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])**.5, delta=.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())
def test_circuit_state_fn_from_complex_vector_initialize(self): """ state fn circuit from complex vector initialize test """ sfc = CircuitStateFn.from_vector( np.array([1j / np.sqrt(2), 0, 1j / np.sqrt(2), 0])) self.assertIsInstance(sfc, CircuitStateFn)