def test_simplify(self): """Test simplify method""" coeffs = [3 + 1j, -3 - 1j, 0, 4, -5, 2.2, -1.1j] labels = ["IXI", "IXI", "ZZZ", "III", "III", "XXX", "XXX"] spp_op = SparsePauliOp.from_list(zip(labels, coeffs)) simplified_op = spp_op.simplify() target_coeffs = [-1, 2.2 - 1.1j] target_labels = ["III", "XXX"] target_op = SparsePauliOp.from_list(zip(target_labels, target_coeffs)) self.assertEqual(simplified_op, target_op) np.testing.assert_array_equal(simplified_op.paulis.phase, np.zeros(simplified_op.size))
def test_eq_equiv(self): """Test __eq__ and equiv methods with some specific cases.""" with self.subTest("shuffled"): spp_op1 = SparsePauliOp.from_list([("X", 1), ("Y", 2)]) spp_op2 = SparsePauliOp.from_list([("Y", 2), ("X", 1)]) self.assertNotEqual(spp_op1, spp_op2) self.assertTrue(spp_op1.equiv(spp_op2)) with self.subTest("w/ zero"): spp_op1 = SparsePauliOp.from_list([("X", 1), ("Y", 1)]) spp_op2 = SparsePauliOp.from_list([("X", 1), ("Y", 1), ("Z", 0)]) self.assertNotEqual(spp_op1, spp_op2) self.assertTrue(spp_op1.equiv(spp_op2))
def test_from_zip(self): """Test from_list method for zipped input.""" labels = ['XXZ', 'IXI', 'YZZ', 'III'] coeffs = [3.0, 5.5, -1j, 23.3333] spp_op = SparsePauliOp.from_list(zip(labels, coeffs)) self.assertTrue(np.array_equal(spp_op.coeffs, coeffs)) self.assertEqual(spp_op.table, PauliTable.from_labels(labels))
def test_from_zip(self): """Test from_list method for zipped input.""" labels = ["XXZ", "IXI", "YZZ", "III"] coeffs = [3.0, 5.5, -1j, 23.3333] spp_op = SparsePauliOp.from_list(zip(labels, coeffs)) np.testing.assert_array_equal(spp_op.coeffs, coeffs) self.assertEqual(spp_op.paulis, PauliList(labels))
def test_from_list(self): """Test from_list method.""" labels = ["XXZ", "IXI", "YZZ", "III"] coeffs = [3.0, 5.5, -1j, 23.3333] spp_op = SparsePauliOp.from_list(list(zip(labels, coeffs))) self.assertTrue(np.array_equal(spp_op.coeffs, coeffs)) self.assertEqual(spp_op.table, PauliTable.from_labels(labels))
def _read_pauli_evolution_gate(file_obj, version, vectors): pauli_evolution_def = formats.PAULI_EVOLUTION_DEF._make( struct.unpack(formats.PAULI_EVOLUTION_DEF_PACK, file_obj.read(formats.PAULI_EVOLUTION_DEF_SIZE))) if pauli_evolution_def.operator_size != 1 and pauli_evolution_def.standalone_op: raise ValueError( "Can't have a standalone operator with {pauli_evolution_raw[0]} operators in the payload" ) operator_list = [] for _ in range(pauli_evolution_def.operator_size): op_elem = formats.SPARSE_PAULI_OP_LIST_ELEM._make( struct.unpack( formats.SPARSE_PAULI_OP_LIST_ELEM_PACK, file_obj.read(formats.SPARSE_PAULI_OP_LIST_ELEM_SIZE), )) op_raw_data = common.data_from_binary(file_obj.read(op_elem.size), np.load) operator_list.append(SparsePauliOp.from_list(op_raw_data)) if pauli_evolution_def.standalone_op: pauli_op = operator_list[0] else: pauli_op = operator_list time = value.loads_value( common.ValueTypeKey(pauli_evolution_def.time_type), file_obj.read(pauli_evolution_def.time_size), version=version, vectors=vectors, ) synth_data = json.loads( file_obj.read(pauli_evolution_def.synth_method_size)) synthesis = getattr(evo_synth, synth_data["class"])(**synth_data["settings"]) return_gate = library.PauliEvolutionGate(pauli_op, time=time, synthesis=synthesis) return return_gate
def test_equiv_atol(self): """Test equiv method with atol.""" op1 = SparsePauliOp.from_list([("X", 1), ("Y", 2)]) op2 = op1 + 1e-7 * SparsePauliOp.from_list([("I", 1)]) self.assertFalse(op1.equiv(op2)) self.assertTrue(op1.equiv(op2, atol=1e-7))