def test_expval(self): """Test expectation_value method""" psi = Statevector([1, 0, 0, 1]) / np.sqrt(2) rho = DensityMatrix(psi) for label, target in [('II', 1), ('XX', 1), ('YY', -1), ('ZZ', 1), ('IX', 0), ('YZ', 0), ('ZX', 0), ('YI', 0)]: with self.subTest(msg="<{}>".format(label)): op = Pauli(label) expval = rho.expectation_value(op) self.assertAlmostEqual(expval, target) psi = Statevector([np.sqrt(2), 0, 0, 0, 0, 0, 0, 1 + 1j]) / 2 rho = DensityMatrix(psi) for label, target in [('XXX', np.sqrt(2) / 2), ('YYY', -np.sqrt(2) / 2), ('ZZZ', 0), ('XYZ', 0), ('YIY', 0)]: with self.subTest(msg="<{}>".format(label)): op = Pauli(label) expval = rho.expectation_value(op) self.assertAlmostEqual(expval, target) labels = ['XXX', 'IXI', 'YYY', 'III'] coeffs = [3.0, 5.5, -1j, 23] spp_op = SparsePauliOp.from_list(list(zip(labels, coeffs))) expval = rho.expectation_value(spp_op) target = 25.121320343559642 + 0.7071067811865476j self.assertAlmostEqual(expval, target)
def test_expval(self): """Test expectation_value method""" psi = Statevector([1, 0, 0, 1]) / np.sqrt(2) rho = DensityMatrix(psi) for label, target in [ ("II", 1), ("XX", 1), ("YY", -1), ("ZZ", 1), ("IX", 0), ("YZ", 0), ("ZX", 0), ("YI", 0), ]: with self.subTest(msg=f"<{label}>"): op = Pauli(label) expval = rho.expectation_value(op) self.assertAlmostEqual(expval, target) psi = Statevector([np.sqrt(2), 0, 0, 0, 0, 0, 0, 1 + 1j]) / 2 rho = DensityMatrix(psi) for label, target in [ ("XXX", np.sqrt(2) / 2), ("YYY", -np.sqrt(2) / 2), ("ZZZ", 0), ("XYZ", 0), ("YIY", 0), ]: with self.subTest(msg=f"<{label}>"): op = Pauli(label) expval = rho.expectation_value(op) self.assertAlmostEqual(expval, target) labels = ["XXX", "IXI", "YYY", "III"] coeffs = [3.0, 5.5, -1j, 23] spp_op = SparsePauliOp.from_list(list(zip(labels, coeffs))) expval = rho.expectation_value(spp_op) target = 25.121320343559642 + 0.7071067811865476j self.assertAlmostEqual(expval, target)