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

MultGate: A gate for multiplying parameters. #182

Merged
merged 6 commits into from
Nov 17, 2024
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
47 changes: 47 additions & 0 deletions src/python/verifier/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,53 @@ def neg(a):
return cos_a, -sin_a


def mult(x, y):
# In this special case, both the lhs and the rhs are numbers.
if isinstance(x, (int, float)) and isinstance(y, (int, float)):
return x * y

# To apply trigonometric angle formulas, one side must be a number.
# Without loss of generality, the left-hand side will be a number.
assert isinstance(x, (int, float)) or isinstance(y, (int, float))
if isinstance(y, (int, float)):
x, y = y, x

# This block ensures that the lhs is not only a number, but also an integer.
# This is because angle-reducing formula only exist for integer multipliers.
# Of course, other formulas exist, such as the half-angle formula.
# However, this formula is not determined (for arbitrary a) by (cos_a, sin_a) alone.
if isinstance(x, float):
assert x.is_integer()
x = int(x)

# Moves negative signs from the left-hand side to the right-hand side.
if x < 0:
x = -x
y = neg(y)

# Base Cases.
if x == 0:
return 1, 0
elif x == 1:
return y
# Triple-angle formula.
elif x % 3 == 0:
cos_y, sin_y = mult(x // 3, y)
cos_z = 4 * cos_y * cos_y * cos_y - 3 * cos_y
sin_z = 3 * sin_y - 4 * sin_y * sin_y * sin_y
return cos_z, sin_z
# Double-angle formula.
elif x % 2 == 0:
cos_y, sin_y = mult(x // 2, y)
cos_z = cos_y * cos_y - sin_y * sin_y
sin_z = 2 * cos_y * sin_y
return cos_z, sin_z
# Otherwise, use the sum formula to decrease x by 1.
else:
z = mult(x - 1, y)
return add(y, z)


# quantum gates


Expand Down
1 change: 1 addition & 0 deletions src/quartz/gate/all_gates.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "h.h"
#include "input_param.h"
#include "input_qubit.h"
#include "mult.h"
#include "neg.h"
#include "p.h"
#include "pdg.h"
Expand Down
1 change: 1 addition & 0 deletions src/quartz/gate/gates.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PER_GATE(rz, RZGate)
PER_GATE(cx, CXGate)
PER_GATE(ccx, CCXGate)
PER_GATE(add, AddGate)
PER_GATE(mult, MultGate)
PER_GATE(neg, NegGate)
PER_GATE(z, ZGate)
PER_GATE(s, SGate)
Expand Down
18 changes: 18 additions & 0 deletions src/quartz/gate/mult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "gate.h"

#include <assert.h>

namespace quartz {
class MultGate : public Gate {
public:
MultGate() : Gate(GateType::mult, 0 /*num_qubits*/, 2 /*num_parameters*/) {}
ParamType compute(const std::vector<ParamType> &input_params) override {
assert(input_params.size() == 2);
return input_params[0] * input_params[1];
}
bool is_commutative() const override { return true; }
};

} // namespace quartz
2 changes: 2 additions & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ file(GLOB_RECURSE TEST_FROM_AND_TO_QASM "test_from_and_to_qasm.cpp")
file(GLOB_RECURSE TEST_OPTIMIZE "test_optimize.cpp")
file(GLOB_RECURSE TEST_CREATE_GRAPHXFER_FROM_QASM "test_create_graphXfer_from_qasm.cpp")
file(GLOB_RECURSE TEST_PARTITION "test_partition.cpp")
file(GLOB_RECURSE TEST_MULT "test_mult.cpp")
if(USE_ARBLIB)
file(GLOB_RECURSE TEST_ARB "test_arb.cpp")
endif()
Expand Down Expand Up @@ -66,6 +67,7 @@ add_executable(test_from_and_to_qasm ${TEST_FROM_AND_TO_QASM} )
add_executable(test_optimize ${TEST_OPTIMIZE} )
add_executable(test_create_graphXfer_from_qasm ${TEST_CREATE_GRAPHXFER_FROM_QASM} )
add_executable(test_partition ${TEST_PARTITION} )
add_executable(test_mult ${TEST_MULT} )
if(USE_ARBLIB)
add_executable(test_arb ${TEST_ARB} )
endif()
31 changes: 31 additions & 0 deletions src/test/test_mult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "quartz/circuitseq/circuitseq.h"
#include "quartz/context/context.h"
#include "quartz/gate/gate.h"

#include <cassert>

using namespace quartz;

int main() {
ParamInfo param_info(0);
Context ctx({GateType::rx, GateType::mult}, 1, &param_info);

auto p0 = ctx.get_new_param_id(2.0);
auto p1 = ctx.get_new_param_id(3.0);
auto p2 = ctx.get_new_param_id(6.0);
auto p3 =
ctx.get_new_param_expression_id({p0, p1}, ctx.get_gate(GateType::mult));

CircuitSeq dag1(1);
dag1.add_gate({0}, {p2}, ctx.get_gate(GateType::rx), &ctx);

CircuitSeq dag2(1);
dag2.add_gate({0}, {p3}, ctx.get_gate(GateType::rx), &ctx);

auto c1 = dag1.to_qasm_style_string(&ctx);
auto c2 = dag2.to_qasm_style_string(&ctx);
assert(c1 == c2);

// Working directory is cmake-build-debug/ here.
system("python ../src/test/test_mult.py");
}
62 changes: 62 additions & 0 deletions src/test/test_mult.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sys

sys.path.append("..")

from src.python.verifier.gates import *


def approx_eq(a, b):
assert len(a) == 2
assert len(b) == 2
cos_a, sin_a = a
cos_b, sin_b = b

err = max(abs(cos_a - cos_b), abs(sin_a - sin_b))
return err < 0.0000000000001


def mult_test(expected, n, a):
actual = mult(n, a)
swapped = mult(a, n)

assert actual == swapped
assert approx_eq(actual, expected)


def test_positive(a):
expected = 1, 0
for n in range(0, 41):
mult_test(expected, n, a)
expected = add(a, expected)


def test_negative(a):
expected = neg(a)
for n in range(-1, -41, -1):
mult_test(expected, n, a)
expected = add(neg(a), expected)


def test_floats(a):
n = 5
expected = mult(n, a)

actual = mult(float(n), a)
swapped = mult(a, float(n))

assert actual == swapped
assert actual == expected


def test_numbers():
assert mult(2, 3.0) == 6.0
assert mult(3.0, 2) == 6.0
assert mult(3, 4) == 12
assert mult(3.0, 0.1) == 0.3


if __name__ == '__main__':
v = 1 / math.sqrt(2)
test_positive((v, v))
test_negative((v, v))
test_floats((v, v))
Loading