Skip to content

Commit 77bc252

Browse files
committed
Changed Order of Inputs for Pauli strings and adjusted Readme to match the new changes
1 parent 14ab10d commit 77bc252

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ The following items are planned or not yet fully implemented:
1717
```python
1818
import pauliengine as pe
1919
# Example : Z on qubit 0 with coefficient 1.0
20-
p1 = pe.PauliString ((1.0, {0: "Z"}))
20+
p1 = pe.PauliString (1.0, {0: "Z"})
2121

2222
# Example : X on qubit 1 with coefficient "a" ( symbolic )
23-
p2 = pe.PauliString (("a", {1: "X"}))
23+
p2 = pe.PauliString ("a", {1: "X"})
2424

2525
# Example : OpenFermion style
2626
p3 = pe.PauliString ((1.0, [("X", 0), ("Y", 2) ]))
2727
```
2828
### Standard Operations
2929
```python
30-
# Addition:
31-
p4 = p2 + p3
30+
3231
# Multiplication
3332
p5 = p4 * p1
3433
# fast commutators
@@ -38,10 +37,10 @@ c = p1.commutator(p2)
3837
### Parametrized Operators
3938
```python
4039
# create PauliString objects
41-
p1 = pe.PauliString ((1.0 , {0: "Z"})) # not parametrized
42-
p2 = pe.PauliString (("a", {1: "X"})) # parametrized
40+
p1 = pe.PauliString (1.0 , {0: "Z"}) # not parametrized
41+
p2 = pe.PauliString ("a", {1: "X"}) # parametrized
4342
# assemble operator
44-
H = QubitHamiltonian([p1 , p2 ])
43+
H = pe.QubitHamiltonian([p1 , p2 ])
4544
```
4645
In beta version, mixed types need to be created like this (create atomic PauliStrings, then assemble operator)
4746

src/PauliString.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PauliString {
5050
}
5151

5252

53-
PauliString(const std::unordered_map<int, std::string>& data, std::complex<double> coeff) {
53+
PauliString(std::complex<double> coeff, const std::unordered_map<int, std::string>& data) {
5454
this->coeff = coeff;
5555
uint64_t mask;
5656
for (const auto& [key, value] : data) {
@@ -72,7 +72,7 @@ class PauliString {
7272
}
7373
}
7474

75-
PauliString(const std::unordered_map<int, std::string>& data, std::string coeff) {
75+
PauliString(std::string coeff, std::unordered_map<int, std::string>& data) {
7676
this->coeff = coeff;
7777
uint64_t mask;
7878
for (const auto& [key, value] : data) {
@@ -97,7 +97,7 @@ class PauliString {
9797
}
9898

9999
#ifdef HAVE_SYMENGINE
100-
PauliString(const std::unordered_map<int, std::string>& data, Expression coeff) {
100+
PauliString(Expression coeff, const std::unordered_map<int, std::string>& data) {
101101
this->coeff = coeff;
102102
uint64_t mask;
103103
for (const auto& [key, value] : data) {
@@ -127,9 +127,9 @@ class PauliString {
127127
this->coeff = coeff;
128128
}
129129

130-
PauliString(const std::pair<std::vector<std::pair<char, int>>, Coeff>& input) {
131-
this->coeff = input.second;
132-
std::vector<std::pair<char, int>> paulis = input.first;
130+
PauliString(const std::pair<std::complex<double>, std::vector<std::pair<char, int>>>& input) {
131+
this->coeff = input.first;
132+
std::vector<std::pair<char, int>> paulis = input.second;
133133
uint64_t mask = 0;
134134
for (int i = 0; i < paulis.size(); i++) {
135135
size_t index = paulis[i].second / BITS_IN_INTEGER;
@@ -148,9 +148,9 @@ class PauliString {
148148
}
149149
}
150150

151-
PauliString(const std::pair<std::vector<std::pair<char, int>>, std::string>& input) {
152-
this->coeff = input.second;
153-
std::vector<std::pair<char, int>> paulis = input.first;
151+
PauliString(const std::pair<std::string, std::vector<std::pair<char, int>>>& input) {
152+
this->coeff = input.first;
153+
std::vector<std::pair<char, int>> paulis = input.second;
154154
uint64_t mask = 0;
155155
for (int i = 0; i < paulis.size(); i++) {
156156
size_t index = paulis[i].second / BITS_IN_INTEGER;
@@ -284,7 +284,7 @@ class PauliString {
284284
mapped[old_idx] = pauli_char;
285285
}
286286
}
287-
return PauliString(mapped, this->coeff);
287+
return PauliString(this->coeff, mapped);
288288
}
289289

290290
std::string to_string() const {

src/QubitHamiltonian.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class QubitHamiltonian {
1515
std::vector<PauliString<>> converted_data;
1616
converted_data.reserve(data.size());
1717
for (Pauli_structure entry : data) {
18-
converted_data.push_back(PauliString<>(entry.second, entry.first));
18+
converted_data.push_back(PauliString<>(entry.first, entry.second));
1919
}
2020
this->data = converted_data;
2121
}
@@ -24,7 +24,7 @@ class QubitHamiltonian {
2424
std::vector<PauliString<>> converted_data;
2525
converted_data.reserve(data.size());
2626
for (Pauli_structure_variable entry : data) {
27-
converted_data.push_back(PauliString<>(entry.second, entry.first));
27+
converted_data.push_back(PauliString<>(entry.first, entry.second));
2828
}
2929
this->data = converted_data;
3030
}
@@ -237,10 +237,14 @@ class QubitHamiltonian {
237237
NB_MODULE(PauliEngine, m) {
238238
nb::class_<PauliString<>>(m, "PauliString", "Represents a Pauli string in binary symplectic form.")
239239
.def(nb::init<>(), "Default constructor.")
240-
.def(nb::init<const std::unordered_map<int, std::string>&, std::complex<double>>(),
241-
"Constructor from a map of qubit indices to Pauli operators and a complex coefficient.")
242-
.def(nb::init<const std::unordered_map<int, std::string>&, std::string>(),
240+
.def(nb::init<const std::string, const std::unordered_map<int, std::string>>(),
243241
"Constructor from a map of qubit indices to Pauli operators and a complex coefficient.")
242+
.def(nb::init<std::complex<double>, const std::unordered_map<int, std::string>>(),
243+
"Constructor from a map of qubit indices to Pauli operators and a symbolic coefficient.")
244+
.def(nb::init<const std::pair<std::complex<double>, std::vector<std::pair<char, int>>>>(),
245+
"Constructor OpenFermion style and a complex coefficient.")
246+
.def(nb::init<const std::pair<std::string, std::vector<std::pair<char, int>>>>(),
247+
"Constructor OpenFermion style and a symbolic coefficient.")
244248
.def("to_string", &PauliString<>::to_string, "Returns a human-readable string representation of the Pauli string.")
245249
.def("is_all_z", &PauliString<>::is_all_z, "Checks if the Pauli string consists only of Z operators.")
246250
.def("get_coeff", &PauliString<>::get_coeff, "Returns the complex coefficient of the Pauli string.")

0 commit comments

Comments
 (0)