def setUp(self): super().setUp() self.ansatz = RealAmplitudes(num_qubits=2, reps=2) self.observable = PauliSumOp.from_list([ ("II", -1.052373245772859), ("IZ", 0.39793742484318045), ("ZI", -0.39793742484318045), ("ZZ", -0.01128010425623538), ("XX", 0.18093119978423156), ]) self.expvals = -1.0284380963435145, -1.284366511861733 self.psi = (RealAmplitudes(num_qubits=2, reps=2), RealAmplitudes(num_qubits=2, reps=3)) self.params = tuple(psi.parameters for psi in self.psi) self.hamiltonian = ( SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)]), SparsePauliOp.from_list([("IZ", 1)]), SparsePauliOp.from_list([("ZI", 1), ("ZZ", 1)]), ) self.theta = ( [0, 1, 1, 2, 3, 5], [0, 1, 1, 2, 3, 5, 8, 13], [1, 2, 3, 4, 5, 6], )
def test_errors(self): """Test for errors""" qc = QuantumCircuit(1) qc2 = QuantumCircuit(2) op = SparsePauliOp.from_list([("I", 1)]) op2 = SparsePauliOp.from_list([("II", 1)]) with self.assertWarns(DeprecationWarning): est = Estimator([qc, qc2], [op, op2], [[]] * 2) with self.assertRaises(QiskitError), self.assertWarns( DeprecationWarning): est([0], [1], [[]]) with self.assertRaises(QiskitError), self.assertWarns( DeprecationWarning): est([1], [0], [[]]) with self.assertRaises(QiskitError), self.assertWarns( DeprecationWarning): est([0], [0], [[1e4]]) with self.assertRaises(QiskitError), self.assertWarns( DeprecationWarning): est([1], [1], [[1, 2]]) with self.assertRaises(QiskitError), self.assertWarns( DeprecationWarning): est([0, 1], [1], [[1]]) with self.assertRaises(QiskitError), self.assertWarns( DeprecationWarning): est([0], [0, 1], [[1]])
def test_1qubit(self): """Test for 1-qubit cases""" qc = QuantumCircuit(1) qc2 = QuantumCircuit(1) qc2.x(0) op = SparsePauliOp.from_list([("I", 1)]) op2 = SparsePauliOp.from_list([("Z", 1)]) with self.assertWarns(DeprecationWarning): est = Estimator([qc, qc2], [op, op2], [[]] * 2) result = est([0], [0], [[]]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) with self.assertWarns(DeprecationWarning): result = est([0], [1], [[]]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) with self.assertWarns(DeprecationWarning): result = est([1], [0], [[]]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) with self.assertWarns(DeprecationWarning): result = est([1], [1], [[]]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [-1])
def test_run_1qubit(self): """Test for 1-qubit cases""" qc = QuantumCircuit(1) qc2 = QuantumCircuit(1) qc2.x(0) op = SparsePauliOp.from_list([("I", 1)]) op2 = SparsePauliOp.from_list([("Z", 1)]) est = Estimator() result = est.run([qc], [op], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc], [op2], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc2], [op], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc2], [op2], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [-1])
def test_run_2qubits(self): """Test for 2-qubit cases (to check endian)""" qc = QuantumCircuit(2) qc2 = QuantumCircuit(2) qc2.x(0) op = SparsePauliOp.from_list([("II", 1)]) op2 = SparsePauliOp.from_list([("ZI", 1)]) op3 = SparsePauliOp.from_list([("IZ", 1)]) est = Estimator() result = est.run([qc], [op], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc2], [op], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc], [op2], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc2], [op2], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc], [op3], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1]) result = est.run([qc2], [op3], [[]]).result() self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [-1])
def test_permute(self, permutation, expected_pauli): """Test the permute method.""" pauli_sum = PauliSumOp(SparsePauliOp.from_list([("XYZ", 1)])) expected = PauliSumOp(SparsePauliOp.from_list([(expected_pauli, 1)])) permuted = pauli_sum.permute(permutation) with self.subTest(msg="test permutated object"): self.assertEqual(permuted, expected) with self.subTest(msg="test original object is unchanged"): original = PauliSumOp(SparsePauliOp.from_list([("XYZ", 1)])) self.assertEqual(pauli_sum, original)
def test_find_Z2_symmetries(self): """test for find_Z2_symmetries""" qubit_op = PauliSumOp.from_list([ ("II", -1.0537076071291125), ("IZ", 0.393983679438514), ("ZI", -0.39398367943851387), ("ZZ", -0.01123658523318205), ("XX", 0.1812888082114961), ]) z2_symmetries = Z2Symmetries.find_Z2_symmetries(qubit_op) self.assertEqual(z2_symmetries.symmetries, [Pauli("ZZ")]) self.assertEqual(z2_symmetries.sq_paulis, [Pauli("IX")]) self.assertEqual(z2_symmetries.sq_list, [0]) self.assertEqual(z2_symmetries.tapering_values, None) tapered_op = z2_symmetries.taper(qubit_op)[1] self.assertEqual(tapered_op.z2_symmetries.symmetries, [Pauli("ZZ")]) self.assertEqual(tapered_op.z2_symmetries.sq_paulis, [Pauli("IX")]) self.assertEqual(tapered_op.z2_symmetries.sq_list, [0]) self.assertEqual(tapered_op.z2_symmetries.tapering_values, [-1]) z2_symmetries.tapering_values = [-1] primitive = SparsePauliOp.from_list([ ("I", -1.0424710218959303), ("Z", -0.7879673588770277), ("X", -0.18128880821149604), ]) expected_op = TaperedPauliSumOp(primitive, z2_symmetries) self.assertEqual(tapered_op, expected_op)
def test_run_errors(self): """Test for errors""" qc = QuantumCircuit(1) qc2 = QuantumCircuit(2) op = SparsePauliOp.from_list([("I", 1)]) op2 = SparsePauliOp.from_list([("II", 1)]) est = Estimator() with self.assertRaises(QiskitError): est.run([qc], [op2], [[]]).result() with self.assertRaises(QiskitError): est.run([qc2], [op], [[]]).result() with self.assertRaises(QiskitError): est.run([qc], [op], [[1e4]]).result() with self.assertRaises(QiskitError): est.run([qc2], [op2], [[1, 2]]).result() with self.assertRaises(QiskitError): est.run([qc, qc2], [op2], [[1]]).result() with self.assertRaises(QiskitError): est.run([qc], [op, op2], [[1]]).result()
def setUp(self): super().setUp() z2_symmetries = Z2Symmetries( [Pauli("IIZI"), Pauli("ZIII")], [Pauli("IIXI"), Pauli("XIII")], [1, 3], [-1, 1]) self.primitive = SparsePauliOp.from_list([ ("II", (-1.052373245772859)), ("ZI", (-0.39793742484318007)), ("IZ", (0.39793742484318007)), ("ZZ", (-0.01128010425623538)), ("XX", (0.18093119978423142)), ]) self.tapered_qubit_op = TaperedPauliSumOp(self.primitive, z2_symmetries)
def from_list( cls, pauli_list: List[Tuple[str, complex]], coeff: Union[complex, ParameterExpression] = 1.0, ) -> "PauliSumOp": """Construct from a pauli_list with the form [(pauli_str, coeffs)] Args: pauli_list: A list of Tuple of pauli_str and coefficient. coeff: A coefficient multiplying the primitive. Returns: The PauliSumOp constructed from the pauli_list. """ return cls(SparsePauliOp.from_list(pauli_list), coeff=coeff)
def _taper(self, op: PauliSumOp, curr_tapering_values: List[int]) -> OperatorBase: pauli_list = [] for pauli_term in op: coeff_out = pauli_term.primitive.coeffs[0] for idx, qubit_idx in enumerate(self._sq_list): if ( pauli_term.primitive.table.Z[0][qubit_idx] or pauli_term.primitive.table.X[0][qubit_idx] ): coeff_out = curr_tapering_values[idx] * coeff_out z_temp = np.delete(pauli_term.primitive.table.Z[0].copy(), np.asarray(self._sq_list)) x_temp = np.delete(pauli_term.primitive.table.X[0].copy(), np.asarray(self._sq_list)) pauli_list.append((Pauli((z_temp, x_temp)).to_label(), coeff_out)) spo = SparsePauliOp.from_list(pauli_list).simplify(atol=0.0) z2_symmetries = self.copy() z2_symmetries.tapering_values = curr_tapering_values return TaperedPauliSumOp(spo, z2_symmetries)
def test_empty_parameter(self): """Test for empty parameter""" n = 2 qc = QuantumCircuit(n) op = SparsePauliOp.from_list([("I" * n, 1)]) with self.assertWarns(DeprecationWarning): estimator = Estimator(circuits=[qc] * 10, observables=[op] * 10) with self.subTest("one circuit"): with self.assertWarns(DeprecationWarning): result = estimator([0], [1], shots=1000) np.testing.assert_allclose(result.values, [1]) self.assertEqual(len(result.metadata), 1) with self.subTest("two circuits"): with self.assertWarns(DeprecationWarning): result = estimator([2, 4], [3, 5], shots=1000) np.testing.assert_allclose(result.values, [1, 1]) self.assertEqual(len(result.metadata), 2)
def test_truncate_tapered_op(self): """Test setting cutoff tolerances for the tapered operator works.""" qubit_op = PauliSumOp.from_list([ ("II", -1.0537076071291125), ("IZ", 0.393983679438514), ("ZI", -0.39398367943851387), ("ZZ", -0.01123658523318205), ("XX", 0.1812888082114961), ]) z2_symmetries = Z2Symmetries.find_Z2_symmetries(qubit_op) z2_symmetries.tol = 0.2 # removes the X part of the tapered op which is < 0.2 tapered_op = z2_symmetries.taper(qubit_op)[1] primitive = SparsePauliOp.from_list([ ("I", -1.0424710218959303), ("Z", -0.7879673588770277), ]) expected_op = TaperedPauliSumOp(primitive, z2_symmetries) self.assertEqual(tapered_op, expected_op)
def test_estimator(self): """test for a simple use case""" lst = [("XX", 1), ("YY", 2), ("ZZ", 3)] with self.subTest("PauliSumOp"): observable = PauliSumOp.from_list(lst) ansatz = RealAmplitudes(num_qubits=2, reps=2) with self.assertWarns(DeprecationWarning): est = Estimator([ansatz], [observable]) result = est([0], [0], parameter_values=[[0, 1, 1, 2, 3, 5]]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1.84209213]) with self.subTest("SparsePauliOp"): observable = SparsePauliOp.from_list(lst) ansatz = RealAmplitudes(num_qubits=2, reps=2) with self.assertWarns(DeprecationWarning): est = Estimator([ansatz], [observable]) result = est([0], [0], parameter_values=[[0, 1, 1, 2, 3, 5]]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1.84209213])
def test_run_numpy_params(self): """Test for numpy array as parameter values""" qc = RealAmplitudes(num_qubits=2, reps=2) op = SparsePauliOp.from_list([("IZ", 1), ("XI", 2), ("ZY", -1)]) k = 5 params_array = np.random.rand(k, qc.num_parameters) params_list = params_array.tolist() params_list_array = list(params_array) estimator = Estimator() target = estimator.run([qc] * k, [op] * k, params_list).result() with self.subTest("ndarrary"): result = estimator.run([qc] * k, [op] * k, params_array).result() self.assertEqual(len(result.metadata), k) np.testing.assert_allclose(result.values, target.values) with self.subTest("list of ndarray"): result = estimator.run([qc] * k, [op] * k, params_list_array).result() self.assertEqual(len(result.metadata), k) np.testing.assert_allclose(result.values, target.values)
def test_convert(self): """ convert test """ qubit_op = PauliSumOp.from_list([ ("IIII", -0.8105479805373266), ("IIIZ", 0.17218393261915552), ("IIZZ", -0.22575349222402472), ("IZZI", 0.1721839326191556), ("ZZII", -0.22575349222402466), ("IIZI", 0.1209126326177663), ("IZZZ", 0.16892753870087912), ("IXZX", -0.045232799946057854), ("ZXIX", 0.045232799946057854), ("IXIX", 0.045232799946057854), ("ZXZX", -0.045232799946057854), ("ZZIZ", 0.16614543256382414), ("IZIZ", 0.16614543256382414), ("ZZZZ", 0.17464343068300453), ("ZIZI", 0.1209126326177663), ]) tapered_qubit_op = TwoQubitReduction(num_particles=2).convert(qubit_op) self.assertIsInstance(tapered_qubit_op, TaperedPauliSumOp) primitive = SparsePauliOp.from_list([ ("II", -1.052373245772859), ("ZI", -0.39793742484318007), ("IZ", 0.39793742484318007), ("ZZ", -0.01128010425623538), ("XX", 0.18093119978423142), ]) symmetries = [Pauli("IIZI"), Pauli("ZIII")] sq_paulis = [Pauli("IIXI"), Pauli("XIII")] sq_list = [1, 3] tapering_values = [-1, 1] z2_symmetries = Z2Symmetries(symmetries, sq_paulis, sq_list, tapering_values) expected_op = TaperedPauliSumOp(primitive, z2_symmetries) self.assertEqual(tapered_qubit_op, expected_op)
def test_numpy_params(self): """Test for numpy array as parameter values""" qc = RealAmplitudes(num_qubits=2, reps=2) op = SparsePauliOp.from_list([("IZ", 1), ("XI", 2), ("ZY", -1)]) k = 5 params_array = np.random.rand(k, qc.num_parameters) params_list = params_array.tolist() params_list_array = list(params_array) with self.assertWarns(DeprecationWarning): estimator = Estimator(circuits=qc, observables=op) target = estimator([0] * k, [0] * k, params_list) with self.subTest("ndarrary"): with self.assertWarns(DeprecationWarning): result = estimator([0] * k, [0] * k, params_array) self.assertEqual(len(result.metadata), k) np.testing.assert_allclose(result.values, target.values) with self.subTest("list of ndarray"): with self.assertWarns(DeprecationWarning): result = estimator([0] * k, [0] * k, params_list_array) self.assertEqual(len(result.metadata), k) np.testing.assert_allclose(result.values, target.values)
def test_h2(self): """Test H2 molecule""" with self.subTest("Excitation edges 1"): assert np.alltrue( bksf._bksf_edge_list_fermionic_op( FermionicOp("+-+-", display_format="dense")) == np.array( [[0, 1], [2, 3]])) with self.subTest("Excitation edges 2"): assert np.alltrue( bksf._bksf_edge_list_fermionic_op( FermionicOp("+--+", display_format="dense")) == np.array( [[0, 1], [3, 2]])) ## H2 from pyscf with sto-3g basis h2_fop = FermionicOp( [ ("+-+-", (0.18128880821149607 + 0j)), ("+--+", (-0.18128880821149607 + 0j)), ("-++-", (-0.18128880821149607 + 0j)), ("-+-+", (0.18128880821149604 + 0j)), ("IIIN", (-0.4759487152209648 + 0j)), ("IINI", (-1.2524635735648986 + 0j)), ("IINN", (0.48217928821207245 + 0j)), ("INII", (-0.4759487152209648 + 0j)), ("ININ", (0.697393767423027 + 0j)), ("INNI", (0.6634680964235684 + 0j)), ("NIII", (-1.2524635735648986 + 0j)), ("NIIN", (0.6634680964235684 + 0j)), ("NINI", (0.6744887663568382 + 0j)), ("NNII", (0.48217928821207245 + 0j)), ], display_format="dense", ) expected_pauli_op = SparsePauliOp.from_list([ ("IIII", (-0.8126179630230767 + 0j)), ("IIZZ", (0.17119774903432952 + 0j)), ("IYYI", (0.04532220205287402 + 0j)), ("IZIZ", (0.17119774903432955 + 0j)), ("IZZI", (0.34297063344496626 + 0j)), ("XIIX", (0.04532220205287402 + 0j)), ("YIIY", (0.04532220205287402 + 0j)), ("YZZY", (0.04532220205287402 + 0j)), ("ZIIZ", (0.3317340482117842 + 0j)), ("ZIZI", (-0.22278593040418454 + 0j)), ("ZXXZ", (0.04532220205287402 + 0j)), ("ZYYZ", (0.04532220205287402 + 0j)), ("ZZII", (-0.22278593040418454 + 0j)), ("ZZZZ", (0.24108964410603623 + 0j)), ]) pauli_sum_op = BravyiKitaevSuperFastMapper().map(h2_fop) op1 = _sort_simplify(expected_pauli_op) op2 = _sort_simplify(pauli_sum_op.primitive) with self.subTest("Map H2 frome sto3g basis, number of terms"): self.assertEqual(len(op1), len(op2)) with self.subTest("Map H2 frome sto3g basis result"): self.assertEqual(op1, op2) with self.subTest("Sparse FermionicOp input"): h2_fop_sparse = h2_fop.to_normal_order() pauli_sum_op_from_sparse = BravyiKitaevSuperFastMapper().map( h2_fop_sparse) op2_from_sparse = _sort_simplify( pauli_sum_op_from_sparse.primitive) self.assertEqual(op1, op2_from_sparse)
def test_estimator_example(self): """test for Estimator example""" psi1 = RealAmplitudes(num_qubits=2, reps=2) psi2 = RealAmplitudes(num_qubits=2, reps=3) params1 = psi1.parameters params2 = psi2.parameters op1 = SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)]) op2 = SparsePauliOp.from_list([("IZ", 1)]) op3 = SparsePauliOp.from_list([("ZI", 1), ("ZZ", 1)]) with self.assertWarns(DeprecationWarning): est = Estimator([psi1, psi2], [op1, op2, op3], [params1, params2]) theta1 = [0, 1, 1, 2, 3, 5] theta2 = [0, 1, 1, 2, 3, 5, 8, 13] theta3 = [1, 2, 3, 4, 5, 6] # calculate [ <psi1(theta1)|op1|psi1(theta1)> ] with self.assertWarns(DeprecationWarning): result = est([0], [0], [theta1]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1.5555572817900956]) self.assertEqual(len(result.metadata), 1) # calculate [ <psi1(theta1)|op2|psi1(theta1)>, <psi1(theta1)|op3|psi1(theta1)> ] with self.assertWarns(DeprecationWarning): result = est([0, 0], [1, 2], [theta1] * 2) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [-0.5516530027638437, 0.07535238795415422]) self.assertEqual(len(result.metadata), 2) # calculate [ <psi2(theta2)|op2|psi2(theta2)> ] with self.assertWarns(DeprecationWarning): result = est([1], [1], [theta2]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [0.17849238433885167]) self.assertEqual(len(result.metadata), 1) # calculate [ <psi1(theta1)|op1|psi1(theta1)>, <psi1(theta3)|op1|psi1(theta3)> ] with self.assertWarns(DeprecationWarning): result = est([0, 0], [0, 0], [theta1, theta3]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [1.5555572817900956, 1.0656325933346835]) self.assertEqual(len(result.metadata), 2) # calculate [ <psi1(theta1)|op1|psi1(theta1)>, # <psi2(theta2)|op2|psi2(theta2)>, # <psi1(theta3)|op3|psi1(theta3)> ] with self.assertWarns(DeprecationWarning): result = est([0, 1, 0], [0, 1, 2], [theta1, theta2, theta3]) self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose( result.values, [1.5555572817900956, 0.17849238433885167, -1.0876631752254926]) self.assertEqual(len(result.metadata), 3) # It is possible to pass objects. # calculate [ <psi2(theta2)|H2|psi2(theta2)> ] with self.assertWarns(DeprecationWarning): result = est([psi2], [op2], [theta2]) np.testing.assert_allclose(result.values, [0.17849238433885167]) self.assertEqual(len(result.metadata), 1)