From 9f2a073fe85e77ffa32423cfd1395f81d37975c7 Mon Sep 17 00:00:00 2001 From: Alec Edgington <54802828+cqc-alec@users.noreply.github.com> Date: Mon, 13 May 2024 10:16:52 +0100 Subject: [PATCH] Escape underscores when converting to latex (#1385) --- pytket/conanfile.py | 2 +- pytket/docs/changelog.rst | 4 ++++ tket/conanfile.py | 2 +- tket/src/Circuit/latex_drawing.cpp | 7 +++++-- tket/test/src/Circuit/test_Circ.cpp | 8 ++++++++ 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pytket/conanfile.py b/pytket/conanfile.py index e222e20965..4360bf35ea 100644 --- a/pytket/conanfile.py +++ b/pytket/conanfile.py @@ -32,7 +32,7 @@ def package(self): cmake.install() def requirements(self): - self.requires("tket/1.2.122@tket/stable") + self.requires("tket/1.2.123@tket/stable") self.requires("tklog/0.3.3@tket/stable") self.requires("tkrng/0.3.3@tket/stable") self.requires("tkassert/0.3.4@tket/stable") diff --git a/pytket/docs/changelog.rst b/pytket/docs/changelog.rst index 5b47149167..b8c043a465 100644 --- a/pytket/docs/changelog.rst +++ b/pytket/docs/changelog.rst @@ -10,6 +10,10 @@ Features: * Add two new status values for circuits on backends: "CANCELLING" and "RETRYING". * Use `lark` package instead of deprecated `lark-parser`. +Fixes: + +* Escape underscores in qubit and bit names when converting to latex. + 1.27.0 (April 2024) ------------------- diff --git a/tket/conanfile.py b/tket/conanfile.py index ed514e1483..91176e68ed 100644 --- a/tket/conanfile.py +++ b/tket/conanfile.py @@ -23,7 +23,7 @@ class TketConan(ConanFile): name = "tket" - version = "1.2.122" + version = "1.2.123" package_type = "library" license = "Apache 2" homepage = "https://github.com/CQCL/tket" diff --git a/tket/src/Circuit/latex_drawing.cpp b/tket/src/Circuit/latex_drawing.cpp index bd47547283..56fab702bc 100644 --- a/tket/src/Circuit/latex_drawing.cpp +++ b/tket/src/Circuit/latex_drawing.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include "tket/Circuit/Boxes.hpp" @@ -287,14 +288,16 @@ std::string Circuit::to_latex_str() const { unsigned n_lines = lines.size(); line_ids.insert({qb, n_lines}); LineBufferInfo& line = *lines.emplace(lines.end()); - line.buffer << "\\lstick{" + qb.repr() + "} & "; + line.buffer << "\\lstick{" + + boost::replace_all_copy(qb.repr(), "_", "\\_") + "} & "; line.is_quantum = true; } for (const Bit& cb : this->all_bits()) { unsigned n_lines = lines.size(); line_ids.insert({cb, n_lines}); LineBufferInfo& line = *lines.emplace(lines.end()); - line.buffer << "\\lstick{" + cb.repr() + "} & "; + line.buffer << "\\lstick{" + + boost::replace_all_copy(cb.repr(), "_", "\\_") + "} & "; line.is_quantum = false; } diff --git a/tket/test/src/Circuit/test_Circ.cpp b/tket/test/src/Circuit/test_Circ.cpp index a59d77692e..c09122a9f2 100644 --- a/tket/test/src/Circuit/test_Circ.cpp +++ b/tket/test/src/Circuit/test_Circ.cpp @@ -37,6 +37,7 @@ #include "tket/Transformations/Replacement.hpp" #include "tket/Transformations/Transform.hpp" #include "tket/Utils/MatrixAnalysis.hpp" +#include "tket/Utils/UnitID.hpp" namespace tket { namespace test_Circ { @@ -2694,6 +2695,13 @@ SCENARIO("Confirm that LaTeX output compiles", "[latex][.long]") { c.add_conditional_gate( OpType::CU3, {1.04, 0.36, -0.36}, {0, 4}, {}, 0); + // https://github.com/CQCL/tket/issues/1363 + Qubit q1("q_1", 0); + Bit c1("c_1", 0); + c.add_qubit(q1); + c.add_bit(c1); + c.add_measure(q1, c1); + c.to_latex_file("circ.tex"); int response = std::system("latexmk -pdf circ.tex -quiet"); REQUIRE(response == 0);