Skip to content

Commit

Permalink
[CircuitSeq] Add a linear function to translate CCZ gates into CX and…
Browse files Browse the repository at this point in the history
… RZ gates (#170)

* [CircuitSeq] Add a linear function to translate Rz gates into T gates

* [CircuitSeq] Add a linear function to translate CCZ gates into CX and RZ gates

* code format
  • Loading branch information
xumingkuan authored Mar 5, 2024
1 parent fefbff2 commit 2dce4f8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
50 changes: 49 additions & 1 deletion src/quartz/circuitseq/circuitseq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ void CircuitSeq::clone_from(const CircuitSeq &other,
}
}

std::unique_ptr<CircuitSeq> CircuitSeq::get_rz_to_t_gates(Context *ctx) const {
std::unique_ptr<CircuitSeq> CircuitSeq::get_rz_to_t(Context *ctx) const {
auto result = std::make_unique<CircuitSeq>(num_qubits, num_input_parameters);
for (auto &gate : gates) {
if (gate->gate->tp == GateType::rz) {
Expand Down Expand Up @@ -1591,6 +1591,54 @@ std::unique_ptr<CircuitSeq> CircuitSeq::get_rz_to_t_gates(Context *ctx) const {
return std::move(result);
}

std::unique_ptr<CircuitSeq> CircuitSeq::get_ccz_to_cx_rz(Context *ctx) const {
auto t = ctx->get_new_param_id(/*is_symbolic=*/false);
ctx->set_param_value(t, 0.25 * PI);
auto tdg = ctx->get_new_param_id(/*is_symbolic=*/false);
ctx->set_param_value(tdg, -0.25 * PI);
auto result =
std::make_unique<CircuitSeq>(num_qubits, ctx->get_num_parameters());
for (auto &gate : gates) {
if (gate->gate->tp == GateType::ccz) {
auto qubit_indices = gate->get_qubit_indices();
auto q0 = qubit_indices[0];
auto q1 = qubit_indices[1];
auto q2 = qubit_indices[2];
bool ok;
ok = result->add_gate({q1, q2}, {}, ctx->get_gate(GateType::cx), nullptr);
assert(ok);
ok = result->add_gate({q2}, {tdg}, ctx->get_gate(GateType::rz), nullptr);
assert(ok);
ok = result->add_gate({q0, q2}, {}, ctx->get_gate(GateType::cx), nullptr);
assert(ok);
ok = result->add_gate({q2}, {t}, ctx->get_gate(GateType::rz), nullptr);
assert(ok);
ok = result->add_gate({q1, q2}, {}, ctx->get_gate(GateType::cx), nullptr);
assert(ok);
ok = result->add_gate({q2}, {tdg}, ctx->get_gate(GateType::rz), nullptr);
assert(ok);
ok = result->add_gate({q0, q2}, {}, ctx->get_gate(GateType::cx), nullptr);
assert(ok);
ok = result->add_gate({q0, q1}, {}, ctx->get_gate(GateType::cx), nullptr);
assert(ok);
ok = result->add_gate({q1}, {tdg}, ctx->get_gate(GateType::rz), nullptr);
assert(ok);
ok = result->add_gate({q0, q1}, {}, ctx->get_gate(GateType::cx), nullptr);
assert(ok);
ok = result->add_gate({q0}, {t}, ctx->get_gate(GateType::rz), nullptr);
assert(ok);
ok = result->add_gate({q1}, {t}, ctx->get_gate(GateType::rz), nullptr);
assert(ok);
ok = result->add_gate({q2}, {t}, ctx->get_gate(GateType::rz), nullptr);
} else {
bool ok;
ok = result->add_gate(gate.get());
assert(ok);
}
}
return std::move(result);
}

void CircuitSeq::remove_quantum_gate_from_graph(
CircuitGate *circuit_gate, bool assert_no_logical_qubit_permutation,
std::unordered_set<CircuitWire *> *output_wires_to_be_removed) {
Expand Down
13 changes: 11 additions & 2 deletions src/quartz/circuitseq/circuitseq.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,19 @@ class CircuitSeq {
const std::vector<int> &param_permutation) const;

/**
* Get a circuit which replaces Rz gates with T, Tdg, S, Sdg, and Z gates.
* Get a circuit which replaces RZ gates with T, Tdg, S, Sdg, and Z gates.
* Requires all Rz gates to have parameters that are multiples of Pi/4.
*/
std::unique_ptr<CircuitSeq> get_rz_to_t_gates(Context *ctx) const;
std::unique_ptr<CircuitSeq> get_rz_to_t(Context *ctx) const;

/**
* Get a circuit which replaces CCZ gates with CX and RZ gates.
* Use only one decomposition:
* ccz q0 q1 q2 = cx q1 q2; rz q2 -0.25pi; cx q0 q2; rz q2 0.25pi; cx q1 q2;
* rz q2 -0.25pi; cx q0 q2; cx q0 q1; rz q1 -0.25pi; cx q0 q1; rz q0 0.25pi;
* rz q1 0.25pi; rz q2 0.25pi;
*/
std::unique_ptr<CircuitSeq> get_ccz_to_cx_rz(Context *ctx) const;

// Returns quantum gates which do not topologically depend on any other
// quantum gates.
Expand Down

0 comments on commit 2dce4f8

Please sign in to comment.