Skip to content

Commit

Permalink
Refactor PauliStrings as templates for easier interop (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
willsimmons1465 authored Oct 31, 2023
1 parent 63c2e03 commit fc43ebf
Show file tree
Hide file tree
Showing 69 changed files with 3,634 additions and 2,800 deletions.
47 changes: 30 additions & 17 deletions pytket/binders/circuit/boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ void init_boxes(py::module &m) {
"An operation defined as the exponential of a tensor of Pauli "
"operations and a (possibly symbolic) phase parameter.")
.def(
py::init<
const py::tket_custom::SequenceVec<Pauli> &, const Expr &,
const CXConfigType &>(),
py::init([](const py::tket_custom::SequenceVec<Pauli> &paulis, Expr t,
CXConfigType config) {
return PauliExpBox(SymPauliTensor(paulis, t), config);
}),
"Construct :math:`e^{-\\frac12 i \\pi t \\sigma_0 \\otimes "
"\\sigma_1 \\otimes \\cdots}` from Pauli operators "
":math:`\\sigma_i \\in \\{I,X,Y,Z\\}` and a parameter "
Expand All @@ -263,10 +264,14 @@ void init_boxes(py::module &m) {
"An operation defined as a pair of exponentials of a tensor of Pauli "
"operations and their (possibly symbolic) phase parameters.")
.def(
py::init<
const py::tket_custom::SequenceVec<Pauli> &, const Expr &,
const py::tket_custom::SequenceVec<Pauli> &, Expr,
CXConfigType>(),
py::init([](const py::tket_custom::SequenceVec<Pauli> &paulis0,
Expr t0,
const py::tket_custom::SequenceVec<Pauli> &paulis1,
Expr t1, CXConfigType config) {
return PauliExpPairBox(
SymPauliTensor(paulis0, t0), SymPauliTensor(paulis1, t1),
config);
}),
"Construct a pair of Pauli exponentials of the form"
" :math:`e^{-\\frac12 i \\pi t_j \\sigma_0 \\otimes "
"\\sigma_1 \\otimes \\cdots}` from Pauli operator strings "
Expand Down Expand Up @@ -297,12 +302,13 @@ void init_boxes(py::module &m) {
.def(
py::init([](const py::tket_custom::SequenceVec<
std::pair<py::tket_custom::SequenceVec<Pauli>, Expr>>
&py_gadgets,
const CXConfigType &cx_config) {
std::vector<std::pair<std::vector<Pauli>, Expr>> gadgets(
std::make_move_iterator(py_gadgets.begin()),
std::make_move_iterator(py_gadgets.end()));
return PauliExpCommutingSetBox(gadgets, cx_config);
&pauli_gadgets,
CXConfigType config) {
std::vector<SymPauliTensor> gadgets;
for (const std::pair<py::tket_custom::SequenceVec<Pauli>, Expr> &g :
pauli_gadgets)
gadgets.push_back(SymPauliTensor(g.first, g.second));
return PauliExpCommutingSetBox(gadgets, config);
}),
"Construct a set of necessarily commuting Pauli exponentials of the "
"form"
Expand All @@ -317,7 +323,14 @@ void init_boxes(py::module &m) {
[](PauliExpCommutingSetBox &pbox) { return *pbox.to_circuit(); },
":return: the :py:class:`Circuit` described by the box")
.def(
"get_paulis", &PauliExpCommutingSetBox::get_pauli_gadgets,
"get_paulis",
[](const PauliExpCommutingSetBox &pbox) {
// For backwards compatibility with before templated PauliTensor
std::vector<std::pair<DensePauliMap, Expr>> gadgets;
for (const SymPauliTensor &g : pbox.get_pauli_gadgets())
gadgets.push_back({g.string, g.coeff});
return gadgets;
},
":return: the corresponding list of Pauli gadgets")
.def(
"get_cx_config", &PauliExpCommutingSetBox::get_cx_config,
Expand Down Expand Up @@ -677,15 +690,15 @@ void init_boxes(py::module &m) {
.def(
py::init([](const py::tket_custom::SequenceVec<std::string>
&pauli_strings) {
PauliStabiliserList stabilisers;
PauliStabiliserVec stabilisers;
for (auto &raw_string : pauli_strings) {
std::vector<Pauli> string;
bool coeff = true;
quarter_turns_t coeff = 0;
for (unsigned i = 0; i < raw_string.size(); i++) {
switch (raw_string[i]) {
case '-':
if (i == 0) {
coeff = false;
coeff = 2;
} else {
throw std::invalid_argument(
"Invalid Pauli string: " + raw_string);
Expand Down
6 changes: 3 additions & 3 deletions pytket/binders/partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ PYBIND11_MODULE(partition, m) {
.def(
"add_result_for_term",
(void(MeasurementSetup::*)(
const QubitPauliString &,
const SpPauliString &,
const MeasurementSetup::MeasurementBitMap &)) &
MeasurementSetup::add_result_for_term,
"Add a new Pauli string with a corresponding BitMap", py::arg("term"),
Expand All @@ -153,7 +153,7 @@ PYBIND11_MODULE(partition, m) {

m.def(
"measurement_reduction",
[](const py::tket_custom::SequenceList<QubitPauliString> &strings,
[](const py::tket_custom::SequenceList<SpPauliString> &strings,
PauliPartitionStrat strat, GraphColourMethod method,
CXConfigType cx_config) {
return measurement_reduction(strings, strat, method, cx_config);
Expand All @@ -173,7 +173,7 @@ PYBIND11_MODULE(partition, m) {

m.def(
"term_sequence",
[](const py::tket_custom::SequenceList<QubitPauliString> &strings,
[](const py::tket_custom::SequenceList<SpPauliString> &strings,
PauliPartitionStrat strat, GraphColourMethod method) {
return term_sequence(strings, strat, method);
},
Expand Down
Loading

0 comments on commit fc43ebf

Please sign in to comment.