Skip to content

Commit

Permalink
Merge branch 'develop' into Update-synthesise_tk-to-better-target-TK2
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdilkes committed Aug 15, 2023
2 parents dcbe538 + 5523f6f commit 6bc680f
Show file tree
Hide file tree
Showing 22 changed files with 514 additions and 102 deletions.
2 changes: 1 addition & 1 deletion pytket/binders/circuit/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void init_library(py::module &m) {
"Equivalent to YYPhase, using a TK2 gate");
library_m.def(
"_YYPhase_using_CX", &CircPool::YYPhase_using_CX,
"Equivalent to YYPhase, using two CX gates and one Rz, one Sdg and one S "
"Equivalent to YYPhase, using two CX gates and one Ry, one Sdg and one S "
"gate.");
library_m.def(
"_ZZPhase_using_TK2", &CircPool::ZZPhase_using_TK2,
Expand Down
9 changes: 8 additions & 1 deletion pytket/binders/circuit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ PYBIND11_MODULE(circuit, m) {
})
.def(
"is_clifford_type",
[](const Op &op) { return op.get_desc().is_clifford_gate(); })
[](const Op &op) { return op.get_desc().is_clifford_gate(); },
"Check if the operation is one of the Clifford `OpType`s.")
.def(
"is_clifford", [](const Op &op) { return op.is_clifford(); },
"Test whether the operation is in the Clifford group. A return value "
"of true guarantees that the operation is Clifford. However, the "
"converse is not the case as some Clifford operations may not be "
"detected as such.")
.def("is_gate", [](const Op &op) { return op.get_desc().is_gate(); });

// NOTE: Sphinx does not automatically pick up the docstring for OpType
Expand Down
2 changes: 1 addition & 1 deletion pytket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def package(self):
cmake.install()

def requirements(self):
self.requires("tket/1.2.32@tket/stable")
self.requires("tket/1.2.33@tket/stable")
self.requires("tklog/0.3.3@tket/stable")
self.requires("tkrng/0.3.3@tket/stable")
self.requires("tkassert/0.3.3@tket/stable")
Expand Down
8 changes: 8 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

Unreleased
----------

Minor new features:

* Implement equality checking for all boxes.
* Add ``Op.is_clifford`` to python binding.

1.18.0 (August 2023)
--------------------

Expand Down
12 changes: 12 additions & 0 deletions pytket/tests/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,17 @@ def test_clifford_checking() -> None:
assert m.is_clifford_type() == False


def test_clifford_evaluation() -> None:
c = Circuit(2, 1)
c.Rx(0, 0).ISWAP(1, 0, 1).Rz(0.3, 0)
rx = c.get_commands()[0].op
assert rx.is_clifford()
iswap = c.get_commands()[1].op
assert iswap.is_clifford()
rz = c.get_commands()[2].op
assert rz.is_clifford() == False


def test_getting_registers() -> None:
c = Circuit(2, 1)
c_regs = c.c_registers
Expand Down Expand Up @@ -1160,6 +1171,7 @@ def test_error_wrong_parameters() -> None:
test_str()
test_phase()
test_clifford_checking()
test_clifford_evaluation()
test_measuring_registers()
test_multi_controlled_gates()
test_counting_n_qubit_gates()
13 changes: 13 additions & 0 deletions pytket/tests/classical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,5 +1185,18 @@ def test_flatten_registers_with_classical_exps() -> None:
assert str(commands[1].op.get_exp()) == "(c[4] | (c[9] ^ c[1]))"


def test_box_equality_check() -> None:
exp1 = Bit(2) & Bit(3)
exp2 = Bit(1) & Bit(3)
exp3 = Bit(1)
ceb1 = ClassicalExpBox(2, 0, 1, exp1)
ceb2 = ClassicalExpBox(2, 0, 1, exp2)
ceb3 = ClassicalExpBox(1, 0, 1, exp3)
assert ceb1 != ceb2
assert ceb1 != ceb3
assert ceb1 == ceb1
assert ceb1 == ClassicalExpBox(2, 0, 1, exp1)


if __name__ == "__main__":
test_wasm()
2 changes: 1 addition & 1 deletion tket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TketConan(ConanFile):
name = "tket"
version = "1.2.32"
version = "1.2.33"
package_type = "library"
license = "Apache 2"
homepage = "https://github.com/CQCL/tket"
Expand Down
42 changes: 8 additions & 34 deletions tket/include/tket/Circuit/Boxes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ class CircBox : public Box {
/**
* Equality check between two CircBox instances
*/
bool is_equal(const Op &op_other) const override {
const CircBox &other = dynamic_cast<const CircBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;

Expand Down Expand Up @@ -211,10 +208,7 @@ class Unitary1qBox : public Box {
/**
* Equality check between two Unitary1qBox instances
*/
bool is_equal(const Op &op_other) const override {
const Unitary1qBox &other = dynamic_cast<const Unitary1qBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the unitary matrix correspnding to this operation */
Eigen::Matrix2cd get_matrix() const { return m_; }
Expand Down Expand Up @@ -276,10 +270,7 @@ class Unitary2qBox : public Box {
/**
* Equality check between two Unitary2qBox instances
*/
bool is_equal(const Op &op_other) const override {
const Unitary2qBox &other = dynamic_cast<const Unitary2qBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the unitary matrix correspnding to this operation */
Eigen::Matrix4cd get_matrix() const { return m_; }
Expand Down Expand Up @@ -338,10 +329,7 @@ class Unitary3qBox : public Box {
/**
* Equality check between two Unitary3qBox instances
*/
bool is_equal(const Op &op_other) const override {
const Unitary3qBox &other = dynamic_cast<const Unitary3qBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the unitary matrix correspnding to this operation */
Matrix8cd get_matrix() const { return m_; }
Expand Down Expand Up @@ -406,10 +394,7 @@ class ExpBox : public Box {
/**
* Equality check between two ExpBox instances
*/
bool is_equal(const Op &op_other) const override {
const ExpBox &other = dynamic_cast<const ExpBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the hermitian matrix and phase parameter */
std::pair<Eigen::Matrix4cd, double> get_matrix_and_phase() const {
Expand Down Expand Up @@ -531,10 +516,7 @@ class QControlBox : public Box {
/**
* Equality check between two QControlBox instances
*/
bool is_equal(const Op &op_other) const override {
const QControlBox &other = dynamic_cast<const QControlBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

std::string get_command_str(const unit_vector_t &args) const override;

Expand Down Expand Up @@ -590,11 +572,7 @@ class ProjectorAssertionBox : public Box {
/**
* Equality check between two ProjectorAssertionBox instances
*/
bool is_equal(const Op &op_other) const override {
const ProjectorAssertionBox &other =
dynamic_cast<const ProjectorAssertionBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the unitary matrix correspnding to this operation */
Eigen::MatrixXcd get_matrix() const { return m_; }
Expand Down Expand Up @@ -646,11 +624,7 @@ class StabiliserAssertionBox : public Box {
/**
* Equality check between two StabiliserAssertionBox instances
*/
bool is_equal(const Op &op_other) const override {
const StabiliserAssertionBox &other =
dynamic_cast<const StabiliserAssertionBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the pauli stabilisers */
PauliStabiliserList get_stabilisers() const { return paulis_; }
Expand Down
2 changes: 1 addition & 1 deletion tket/include/tket/Circuit/CircPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Circuit XXPhase_using_CX(const Expr &alpha);
/** Equivalent to YYPhase, using a TK2 gate */
Circuit YYPhase_using_TK2(const Expr &alpha);

/** Equivalent to YYPhase, using two CX gates and one Rz
/** Equivalent to YYPhase, using two CX gates and one Ry
* one Sdg and one S gate.
*/
Circuit YYPhase_using_CX(const Expr &alpha);
Expand Down
5 changes: 3 additions & 2 deletions tket/include/tket/Circuit/ClassicalExpBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ class ClassicalExpBox : public ClassicalExpBoxBase {
SymSet free_symbols() const override { return SymSet(); }

/**
* Equality check between two CircBox instances
* Equality check between two ClassicalExpBox instances
*/
bool is_equal(const Op &op_other) const override {
const ClassicalExpBox &other =
dynamic_cast<const ClassicalExpBox &>(op_other);
return id_ == other.get_id();
if (id_ == other.get_id()) return true;
return content_equality(other);
}

op_signature_t get_signature() const override { return sig_; }
Expand Down
5 changes: 1 addition & 4 deletions tket/include/tket/Circuit/DiagonalBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ class DiagonalBox : public Box {
/**
* Equality check between two DiagonalBox instances
*/
bool is_equal(const Op &op_other) const override {
const DiagonalBox &other = dynamic_cast<const DiagonalBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;
Op_ptr transpose() const override;
Expand Down
24 changes: 4 additions & 20 deletions tket/include/tket/Circuit/Multiplexor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ class MultiplexorBox : public Box {
/**
* Equality check between two MultiplexorBox instances
*/
bool is_equal(const Op &op_other) const override {
const MultiplexorBox &other =
dynamic_cast<const MultiplexorBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;

Expand Down Expand Up @@ -129,11 +125,7 @@ class MultiplexedRotationBox : public Box {
/**
* Equality check between two MultiplexedRotationBox instances
*/
bool is_equal(const Op &op_other) const override {
const MultiplexedRotationBox &other =
dynamic_cast<const MultiplexedRotationBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;

Expand Down Expand Up @@ -195,11 +187,7 @@ class MultiplexedU2Box : public Box {
/**
* Equality check between two MultiplexedU2Box instances
*/
bool is_equal(const Op &op_other) const override {
const MultiplexedU2Box &other =
dynamic_cast<const MultiplexedU2Box &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;

Expand Down Expand Up @@ -269,11 +257,7 @@ class MultiplexedTensoredU2Box : public Box {
/**
* Equality check between two MultiplexedTensoredU2Box instances
*/
bool is_equal(const Op &op_other) const override {
const MultiplexedTensoredU2Box &other =
dynamic_cast<const MultiplexedTensoredU2Box &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;

Expand Down
15 changes: 3 additions & 12 deletions tket/include/tket/Circuit/PauliExpBoxes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ class PauliExpBox : public Box {
/**
* Equality check between two PauliExpBox instances
*/
bool is_equal(const Op &op_other) const override {
const auto &other = dynamic_cast<const PauliExpBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the Pauli string */
std::vector<Pauli> get_paulis() const { return paulis_; }
Expand Down Expand Up @@ -117,10 +114,7 @@ class PauliExpPairBox : public Box {
/**
* Equality check between two instances
*/
bool is_equal(const Op &op_other) const override {
const auto &other = dynamic_cast<const PauliExpPairBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get Pauli strings for the pair */
std::pair<std::vector<Pauli>, std::vector<Pauli>> get_paulis_pair() const {
Expand Down Expand Up @@ -184,10 +178,7 @@ class PauliExpCommutingSetBox : public Box {
/**
* Equality check between two instances
*/
bool is_equal(const Op &op_other) const override {
const auto &other = dynamic_cast<const PauliExpCommutingSetBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

/** Get the pauli gadgets */
auto get_pauli_gadgets() const { return pauli_gadgets_; }
Expand Down
6 changes: 1 addition & 5 deletions tket/include/tket/Circuit/StatePreparation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ class StatePreparationBox : public Box {
/**
* Equality check between two StatePreparationBox instances
*/
bool is_equal(const Op &op_other) const override {
const StatePreparationBox &other =
dynamic_cast<const StatePreparationBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

Op_ptr dagger() const override;

Expand Down
5 changes: 1 addition & 4 deletions tket/include/tket/Circuit/ToffoliBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ class ToffoliBox : public Box {
/**
* Equality check between two ToffoliBox instances
*/
bool is_equal(const Op &op_other) const override {
const ToffoliBox &other = dynamic_cast<const ToffoliBox &>(op_other);
return id_ == other.get_id();
}
bool is_equal(const Op &op_other) const override;

std::optional<Eigen::MatrixXcd> get_box_unitary() const override;

Expand Down
Loading

0 comments on commit 6bc680f

Please sign in to comment.