Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stringifications of ops match OpType names. #175

Merged
merged 3 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pytket/binders/circuit/Circuit/add_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ void init_circuit_add_op(py::class_<Circuit, std::shared_ptr<Circuit>> &c) {
const std::vector<Expr> &params,
const std::vector<unsigned> &qubits, const py::kwargs &kwargs) {
return add_box_method<unsigned>(
circ, std::make_shared<CompositeGate>(def, params), qubits,
circ, std::make_shared<CustomGate>(def, params), qubits,
kwargs);
},
"Append an instance of a :py:class:`CustomGateDef` to the "
Expand Down Expand Up @@ -542,7 +542,7 @@ void init_circuit_add_op(py::class_<Circuit, std::shared_ptr<Circuit>> &c) {
const std::vector<Expr> &params, const qubit_vector_t &qubits,
const py::kwargs &kwargs) {
return add_box_method<UnitID>(
circ, std::make_shared<CompositeGate>(def, params),
circ, std::make_shared<CustomGate>(def, params),
{qubits.begin(), qubits.end()}, kwargs);
},
"Append an instance of a :py:class:`CustomGateDef` to the "
Expand Down
3 changes: 1 addition & 2 deletions pytket/binders/circuit/Circuit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,7 @@ void init_circuit(py::module &m) {
py::arg("box"), py::arg("opgroup"))
.def(
"substitute_named",
[](Circuit &circ, const CompositeGate &box,
const std::string opgroup) {
[](Circuit &circ, const CustomGate &box, const std::string opgroup) {
return circ.substitute_named(box, opgroup);
},
"Substitute all ops with the given name for the given box."
Expand Down
10 changes: 5 additions & 5 deletions pytket/binders/circuit/boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ void init_boxes(py::module &m) {
.def_property_readonly(
"arity", &CompositeGateDef::n_args,
"The number of real parameters for the gate");
py::class_<CompositeGate, std::shared_ptr<CompositeGate>, Op>(
py::class_<CustomGate, std::shared_ptr<CustomGate>, Op>(
m, "CustomGate",
"A user-defined gate defined by a parametrised :py:class:`Circuit`.")
.def_property_readonly(
"name", &CompositeGate::get_name, "The readable name of the gate.")
"name", &CustomGate::get_name, "The readable name of the gate.")
.def_property_readonly(
"params", &CompositeGate::get_params, "The parameters of the gate.")
"params", &CustomGate::get_params, "The parameters of the gate.")
.def_property_readonly(
"gate", &CompositeGate::get_gate, "Underlying gate object.")
"gate", &CustomGate::get_gate, "Underlying gate object.")
.def(
"get_circuit",
[](CompositeGate &composite) { return *composite.to_circuit(); },
[](CustomGate &composite) { return *composite.to_circuit(); },
":return: the :py:class:`Circuit` described by the gate.");
py::class_<PhasePolyBox, std::shared_ptr<PhasePolyBox>, Op>(
m, "PhasePolyBox",
Expand Down
6 changes: 3 additions & 3 deletions pytket/binders/circuit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ PYBIND11_MODULE(circuit, m) {
"\\mathrm{Rz}(\\phi) \\mathrm{Ry}(\\theta) "
"\\mathrm{Rz}(\\lambda)`")
.value(
"TK1", OpType::tk1,
"TK1", OpType::TK1,
":math:`(\\alpha, \\beta, \\gamma) \\mapsto "
"\\mathrm{Rz}(\\alpha) \\mathrm{Rx}(\\beta) "
"\\mathrm{Rz}(\\gamma)`")
Expand Down Expand Up @@ -306,14 +306,14 @@ PYBIND11_MODULE(circuit, m) {
"QControlBox", OpType::QControlBox,
"An arbitrary n-controlled operation")
.value(
"Custom", OpType::Composite,
"CustomGate", OpType::CustomGate,
":math:`(\\alpha, \\beta, \\ldots) \\mapsto` A user-defined "
"operation, based on a :py:class:`Circuit` :math:`C` with "
"parameters :math:`\\alpha, \\beta, \\ldots` substituted in "
"place of bound symbolic variables in :math:`C`, as defined "
"by the :py:class:`CustomGateDef`.")
.value(
"ConditionalGate", OpType::Conditional,
"Conditional", OpType::Conditional,
"An operation to be applied conditionally on the value of "
"some classical register")
.value(
Expand Down
4 changes: 2 additions & 2 deletions pytket/binders/passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ PYBIND11_MODULE(passes, m) {
"RebasePyZX", &RebasePyZX,
"Converts all gates to SWAP, CX, CZ, H, X, Z, S, T, Rx and Rz.");
m.def("RebaseQuil", &RebaseQuil, "Converts all gates to CZ, Rx and Rz.");
m.def("RebaseTket", &RebaseTket, "Converts all gates to CX and tk1.");
m.def("RebaseTket", &RebaseTket, "Converts all gates to CX and TK1.");
m.def(
"RebaseUMD", &RebaseUMD,
"Converts all gates to XXPhase, PhasedX and Rz.");
Expand Down Expand Up @@ -587,7 +587,7 @@ PYBIND11_MODULE(passes, m) {
"simplifying Clifford gate sequences, similar to Duncan & Fagan "
"(https://arxiv.org/abs/1901.10114). "
"Given a circuit with CXs and any single-qubit gates, produces a "
"circuit with tk1, CX gates."
"circuit with TK1, CX gates."
"\n\n:param allow_swaps: dictates whether the rewriting will "
"disregard CX placement or orientation and introduce wire swaps."
"\n:return: a pass to perform the rewriting",
Expand Down
4 changes: 2 additions & 2 deletions pytket/binders/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ PYBIND11_MODULE(transform, m) {
.def_static(
"RebaseToCliffordSingles", &Transform::decompose_cliffords_std,
"Identify Clifford-angle rotations (from U1, U2, U3, Rx, "
"Ry, Rz, tk1, PhasedX), replacing them with Z, X, S, V "
"Ry, Rz, TK1, PhasedX), replacing them with Z, X, S, V "
"gates. Any non-Clifford rotations will stay as they are.")
.def_static(
"RebaseToCirq", &Transform::rebase_cirq,
Expand Down Expand Up @@ -217,7 +217,7 @@ PYBIND11_MODULE(transform, m) {
"corresponding to any non-Clifford rotations and "
"synthesises them pairwise (see Cowtan, Duncan, Dilkes, "
"Simmons, & Sivarajah https://arxiv.org/abs/1906.01734). "
"Results use tk1, CX gates.",
"Results use TK1, CX gates.",
py::arg("cx_config") = CXConfigType::Snake)
.def_static(
"RemoveRedundancies", &Transform::remove_redundancies,
Expand Down
2 changes: 1 addition & 1 deletion pytket/pytket/circuit/decompose_classical.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def _decompose_expressions(circ: Circuit) -> Tuple[Circuit, bool]:
optype = op.type
args = command.args
kwargs = dict()
if optype == OpType.ConditionalGate:
if optype == OpType.Conditional:
bits = args[: op.width]
# check if conditional on previously decomposed expression
if len(bits) == 1 and bits[0] in replace_targets:
Expand Down
11 changes: 4 additions & 7 deletions pytket/pytket/circuit/display/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
Circuit,
Command,
Op,
QControlBox,
ExpBox,
BitRegister,
QubitRegister,
Expand Down Expand Up @@ -215,7 +214,7 @@ def has_gate_info(op_type: str) -> bool:
"ClassicalExpBox",
"Custom",
"CircBox",
"ConditionalGate",
"Conditional",
"QControlBox",
}

Expand Down Expand Up @@ -281,7 +280,7 @@ def is_control_gate(op_type: str) -> bool:
"CCX",
"Control",
"QControlBox",
"ConditionalGate",
"Conditional",
}


Expand Down Expand Up @@ -323,7 +322,7 @@ def convert(control_type: str, op: Optional[Op]) -> Tuple[str, Optional[Op]]:
if control_type == "QControlBox":
sub_op = op.get_op()
return sub_op.type.name, sub_op
elif control_type == "ConditionalGate":
elif control_type == "Conditional":
return op.op.type.name, op.op
return "Unknown", None

Expand Down Expand Up @@ -376,9 +375,7 @@ def get_controlled_ops(op_type: str, command: Command) -> int:
return 2
elif op_type == "QControlBox":
return int(command.op.get_n_controls())
elif op_type == "Control" and isinstance(command.op.box, QControlBox):
return int(command.op.box.n_controls)
elif op_type == "ConditionalGate":
elif op_type == "Conditional":
return int(command.op.width)

return 0
Expand Down
2 changes: 1 addition & 1 deletion pytket/pytket/qasm/qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def circuit_to_qasm_io(
# attach predicate to bit,
# subsequent conditional will handle it
continue
if optype == OpType.ConditionalGate:
if optype == OpType.Conditional:
bits = args[: op.width]
control_bit = bits[0]
if control_bit in range_preds:
Expand Down
2 changes: 1 addition & 1 deletion pytket/tests/predicates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_rebase_pass_generation() -> None:
seq = SequencePass(passlist)
assert seq.apply(cu)
coms = cu.circuit.get_commands()
assert str(coms) == "[tk1(0.5, 1, 0.5) q[0];, tk1(0.5, 1, 3.5) q[1];]"
assert str(coms) == "[TK1(0.5, 1, 0.5) q[0];, TK1(0.5, 1, 3.5) q[1];]"


def test_custom_combinator_generation() -> None:
Expand Down
30 changes: 14 additions & 16 deletions tket/src/Circuit/Boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void Unitary1qBox::generate_circuit() const {
std::vector<double> tk1_params = tk1_angles_from_unitary(m_);
Circuit temp_circ(1);
temp_circ.add_op<unsigned>(
OpType::tk1, {tk1_params[0], tk1_params[1], tk1_params[2]}, {0});
OpType::TK1, {tk1_params[0], tk1_params[1], tk1_params[2]}, {0});
circ_ = std::make_shared<Circuit>(temp_circ);
circ_->add_phase(tk1_params[3]);
}
Expand Down Expand Up @@ -263,33 +263,31 @@ bool CompositeGateDef::operator==(const CompositeGateDef &other) const {
return this->get_def()->circuit_equality(*other.get_def(), {}, false);
}

CompositeGate::CompositeGate(
CustomGate::CustomGate(
const composite_def_ptr_t &gate, const std::vector<Expr> &params)
: Box(OpType::Composite, gate->signature()), gate_(gate), params_(params) {
: Box(OpType::CustomGate, gate->signature()), gate_(gate), params_(params) {
if (params_.size() != gate_->n_args()) throw InvalidParameterCount();
}

CompositeGate::CompositeGate(const CompositeGate &other)
CustomGate::CustomGate(const CustomGate &other)
: Box(other), gate_(other.gate_), params_(other.params_) {}

Op_ptr CompositeGate::symbol_substitution(
Op_ptr CustomGate::symbol_substitution(
const SymEngine::map_basic_basic &sub_map) const {
std::vector<Expr> new_params;
for (const Expr &p : this->params_) {
new_params.push_back(p.subs(sub_map));
}
return std::make_shared<CompositeGate>(this->gate_, new_params);
return std::make_shared<CustomGate>(this->gate_, new_params);
}

void CompositeGate::generate_circuit() const {
void CustomGate::generate_circuit() const {
circ_ = std::make_shared<Circuit>(gate_->instance(params_));
}

SymSet CompositeGate::free_symbols() const {
return to_circuit()->free_symbols();
}
SymSet CustomGate::free_symbols() const { return to_circuit()->free_symbols(); }

std::string CompositeGate::get_name(bool) const {
std::string CustomGate::get_name(bool) const {
std::stringstream s;
s << gate_->get_name();
if (!params_.empty()) {
Expand Down Expand Up @@ -556,16 +554,16 @@ void from_json(const nlohmann::json &j, composite_def_ptr_t &cdef) {
j.at("args").get<std::vector<Sym>>());
}

nlohmann::json CompositeGate::to_json(const Op_ptr &op) {
const auto &box = static_cast<const CompositeGate &>(*op);
nlohmann::json CustomGate::to_json(const Op_ptr &op) {
const auto &box = static_cast<const CustomGate &>(*op);
nlohmann::json j = core_box_json(box);
j["gate"] = box.get_gate();
j["params"] = box.get_params();
return j;
}

Op_ptr CompositeGate::from_json(const nlohmann::json &j) {
CompositeGate box = CompositeGate(
Op_ptr CustomGate::from_json(const nlohmann::json &j) {
CustomGate box = CustomGate(
j.at("gate").get<composite_def_ptr_t>(),
j.at("params").get<std::vector<Expr>>());
return set_box_id(
Expand Down Expand Up @@ -626,7 +624,7 @@ REGISTER_OPFACTORY(Unitary2qBox, Unitary2qBox)
REGISTER_OPFACTORY(Unitary3qBox, Unitary3qBox)
REGISTER_OPFACTORY(ExpBox, ExpBox)
REGISTER_OPFACTORY(PauliExpBox, PauliExpBox)
REGISTER_OPFACTORY(Composite, CompositeGate)
REGISTER_OPFACTORY(CustomGate, CustomGate)
REGISTER_OPFACTORY(QControlBox, QControlBox)
REGISTER_OPFACTORY(ProjectorAssertionBox, ProjectorAssertionBox)
REGISTER_OPFACTORY(StabiliserAssertionBox, StabiliserAssertionBox)
Expand Down
13 changes: 6 additions & 7 deletions tket/src/Circuit/Boxes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,10 @@ class CompositeGateDef : public std::enable_shared_from_this<CompositeGateDef> {
CompositeGateDef() {}
};

class CompositeGate : public Box {
class CustomGate : public Box {
public:
CompositeGate(
const composite_def_ptr_t &gate, const std::vector<Expr> &params);
CompositeGate(const CompositeGate &other);
CustomGate(const composite_def_ptr_t &gate, const std::vector<Expr> &params);
CustomGate(const CustomGate &other);

SymSet free_symbols() const override;

Expand All @@ -510,10 +509,10 @@ class CompositeGate : public Box {
const SymEngine::map_basic_basic &sub_map) const override;

/**
* Equality check between two CompositeGate instances
* Equality check between two CustomGate instances
*/
bool is_equal(const Op &op_other) const override {
const CompositeGate &other = dynamic_cast<const CompositeGate &>(op_other);
const CustomGate &other = dynamic_cast<const CustomGate &>(op_other);
return this->id_ == other.id_;
}

Expand All @@ -523,7 +522,7 @@ class CompositeGate : public Box {

protected:
void generate_circuit() const override;
CompositeGate() : Box(OpType::Composite), gate_(), params_() {}
CustomGate() : Box(OpType::CustomGate), gate_(), params_() {}

private:
composite_def_ptr_t gate_;
Expand Down
10 changes: 5 additions & 5 deletions tket/src/Circuit/CircUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace tket {

Eigen::Matrix2cd get_matrix(const Circuit &circ, const Vertex &vert) {
const Op_ptr op = circ.get_Op_ptr_from_Vertex(vert);
if (op->get_type() != OpType::tk1) {
if (op->get_type() != OpType::TK1) {
throw NotImplemented(
"Cannot obtain matrix from gate: " + op->get_name() +
". Try rebasing to tket's internal representation.");
Expand Down Expand Up @@ -156,10 +156,10 @@ Circuit two_qubit_canonical(const Eigen::Matrix4cd &U, double cx_fidelity) {
if (gates.empty()) {
std::vector<double> angles_q0 = tk1_angles_from_unitary(K1a * K2a);
result.add_op<unsigned>(
OpType::tk1, {angles_q0.begin(), angles_q0.end() - 1}, {0});
OpType::TK1, {angles_q0.begin(), angles_q0.end() - 1}, {0});
std::vector<double> angles_q1 = tk1_angles_from_unitary(K1b * K2b);
result.add_op<unsigned>(
OpType::tk1, {angles_q1.begin(), angles_q1.end() - 1}, {1});
OpType::TK1, {angles_q1.begin(), angles_q1.end() - 1}, {1});
}
for (auto it = gates.begin(); it != gates.end(); ++it) {
auto [ga, gb] = *it;
Expand All @@ -175,10 +175,10 @@ Circuit two_qubit_canonical(const Eigen::Matrix4cd &U, double cx_fidelity) {
}
std::vector<double> angles_q0 = tk1_angles_from_unitary(ga);
result.add_op<unsigned>(
OpType::tk1, {angles_q0.begin(), angles_q0.end() - 1}, {0});
OpType::TK1, {angles_q0.begin(), angles_q0.end() - 1}, {0});
std::vector<double> angles_q1 = tk1_angles_from_unitary(gb);
result.add_op<unsigned>(
OpType::tk1, {angles_q1.begin(), angles_q1.end() - 1}, {1});
OpType::TK1, {angles_q1.begin(), angles_q1.end() - 1}, {1});
if (it + 1 != gates.end()) {
result.add_op<unsigned>(OpType::CX, {0, 1});
}
Expand Down
8 changes: 4 additions & 4 deletions tket/src/Circuit/CircUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
namespace tket {

/**
* Convert a vertex holding a tk1 operation to its corresponding matrix
* Convert a vertex holding a TK1 operation to its corresponding matrix
*
* @param circ circuit
* @param vert vertex in circuit
*
* @pre \p vert is an operation of \ref OpType::tk1
* @pre \p vert is an operation of \ref OpType::TK1
* @pre operation has no symbolic parameters
*
* @return corresponding unitary matrix
*/
Eigen::Matrix2cd get_matrix(const Circuit& circ, const Vertex& vert);

/**
* Convert a one-qubit circuit of tk1 operations to its corresponding matrix
* Convert a one-qubit circuit of TK1 operations to its corresponding matrix
*
* @param circ circuit
*
* @pre all vertices are operations of \ref OpType::tk1
* @pre all vertices are operations of \ref OpType::TK1
* @pre circuit has no symbolic parameters
*
* @return corresponding unitary matrix
Expand Down
2 changes: 1 addition & 1 deletion tket/src/Circuit/ThreeQubitConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static std::optional<std::pair<Circuit, Circuit>> separate_0_12(
if (U.isApprox(Eigen::KroneckerProduct(W, V))) {
std::vector<double> angs = tk1_angles_from_unitary(W);
Circuit c_1q(1);
c_1q.add_op<unsigned>(OpType::tk1, {angs[0], angs[1], angs[2]}, {0});
c_1q.add_op<unsigned>(OpType::TK1, {angs[0], angs[1], angs[2]}, {0});
c_1q.add_phase(angs[3]);
Circuit c_2q = two_qubit_canonical(V);
return std::pair<Circuit, Circuit>(c_1q, c_2q);
Expand Down
2 changes: 1 addition & 1 deletion tket/src/Circuit/ThreeQubitConversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace tket {
/**
* Synthesise a 3-qubit circuit from an arbitrary 8x8 unitary.
*
* The returned circuit consists of CX, tk1, H, Ry and Rz gates only. It
* The returned circuit consists of CX, TK1, H, Ry and Rz gates only. It
* contains a maximum of 20 CX gates.
*
* @param U unitary matrix in \ref BasisOrder::ilo
Expand Down
Loading