Skip to content

Commit 8495cdb

Browse files
committed
Add test for qasm3 export
1 parent a1f1210 commit 8495cdb

1 file changed

Lines changed: 112 additions & 2 deletions

File tree

test/test_circuit.cpp

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ static int test_compose(void) {
612612
return Ok;
613613
}
614614

615-
static int test_to_qasm3_multi_regs(void) {
615+
static int test_to_qasm3(void) {
616616
auto qreg1 = QuantumRegister(2, std::string("q1"));
617617
auto qreg2 = QuantumRegister(1, std::string("q2"));
618618
auto creg1 = ClassicalRegister(2, std::string("c1"));
@@ -641,6 +641,113 @@ static int test_to_qasm3_multi_regs(void) {
641641
return Ok;
642642
}
643643

644+
static int test_to_qasm3_multi_regs(void) {
645+
auto qreg1 = QuantumRegister(2, std::string("q1"));
646+
auto qreg2 = QuantumRegister(1, std::string("q2"));
647+
auto creg1 = ClassicalRegister(2, std::string("c1"));
648+
auto creg2 = ClassicalRegister(1, std::string("c2"));
649+
QuantumCircuit circ(std::vector<QuantumRegister>({qreg1, qreg2}),
650+
std::vector<ClassicalRegister>({creg1, creg2}));
651+
652+
circ.measure(qreg2, creg2);
653+
circ.measure(qreg1, creg1);
654+
655+
const auto actual = circ.to_qasm3();
656+
const std::string expected = "OPENQASM 3.0;\n"
657+
"include \"stdgates.inc\";\n"
658+
"qubit[3] q;\n"
659+
"bit[2] c1;\n"
660+
"bit[1] c2;\n"
661+
"c2[0] = measure q[2];\n"
662+
"c1[0] = measure q[0];\n"
663+
"c1[1] = measure q[1];\n";
664+
if (actual != expected) {
665+
std::cerr << " to_qasm3_multi_regs test : \n expected:\n"
666+
<< expected << "\n actual:\n"
667+
<< actual << std::endl;
668+
return EqualityError;
669+
}
670+
671+
return Ok;
672+
}
673+
674+
static int test_to_qasm3_single_param(void) {
675+
auto circ = QuantumCircuit(1, 0);
676+
Parameter theta("theta");
677+
circ.rx(theta, 0);
678+
679+
const auto actual = circ.to_qasm3();
680+
const std::string expected = "OPENQASM 3.0;\n"
681+
"include \"stdgates.inc\";\n"
682+
"input float[64] theta;\n"
683+
"qubit[1] q;\n"
684+
"rx(theta) q[0];\n";
685+
if (actual != expected) {
686+
std::cerr << " to_qasm3_single_param test : \n expected:\n"
687+
<< expected << "\n actual:\n"
688+
<< actual << std::endl;
689+
return EqualityError;
690+
}
691+
return Ok;
692+
}
693+
694+
static int test_to_qasm3_multiple_params(void) {
695+
auto circ = QuantumCircuit(2, 0);
696+
Parameter alpha("alpha");
697+
Parameter beta("beta");
698+
circ.rx(alpha, 0);
699+
circ.ry(beta, 1);
700+
701+
const auto actual = circ.to_qasm3();
702+
const std::string expected = "OPENQASM 3.0;\n"
703+
"include \"stdgates.inc\";\n"
704+
"input float[64] alpha;\n"
705+
"input float[64] beta;\n"
706+
"qubit[2] q;\n"
707+
"rx(alpha) q[0];\n"
708+
"ry(beta) q[1];\n";
709+
if (actual != expected) {
710+
std::cerr << " to_qasm3_multiple_params test : \n expected:\n"
711+
<< expected << "\n actual:\n"
712+
<< actual << std::endl;
713+
return EqualityError;
714+
}
715+
return Ok;
716+
}
717+
718+
static int test_to_qasm3_expression_param(void) {
719+
auto circ = QuantumCircuit(1, 0);
720+
Parameter t("t");
721+
Parameter expr = t + Parameter(0.5);
722+
circ.rz(expr, 0);
723+
724+
const auto actual = circ.to_qasm3();
725+
// The expression "0.5 + t" (or "t + 0.5") should cause only "t" to be
726+
// declared as input, not the numeric literal.
727+
if (actual.find("input float[64] t;") == std::string::npos) {
728+
std::cerr
729+
<< " to_qasm3_expression_param test : missing 'input float[64] t;'\n"
730+
<< " actual:\n"
731+
<< actual << std::endl;
732+
return EqualityError;
733+
}
734+
735+
size_t count = 0;
736+
size_t pos = 0;
737+
while ((pos = actual.find("input float[64]", pos)) != std::string::npos) {
738+
count++;
739+
pos += 15;
740+
}
741+
if (count != 1) {
742+
std::cerr << " to_qasm3_expression_param test : expected 1 input "
743+
"declaration, got "
744+
<< count << "\n actual:\n"
745+
<< actual << std::endl;
746+
return EqualityError;
747+
}
748+
return Ok;
749+
}
750+
644751
#if defined(_WIN32)
645752
int test_circuit(int argc, char** const argv) {
646753
#else
@@ -651,7 +758,10 @@ int test_circuit(int argc, char** argv) {
651758
num_failed += RUN_TEST(test_measure);
652759
num_failed += RUN_TEST(test_append);
653760
num_failed += RUN_TEST(test_compose);
654-
num_failed += RUN_TEST(test_to_qasm3_multi_regs);
761+
num_failed += RUN_TEST(test_to_qasm3);
762+
num_failed += RUN_TEST(test_to_qasm3_single_param);
763+
num_failed += RUN_TEST(test_to_qasm3_multiple_params);
764+
num_failed += RUN_TEST(test_to_qasm3_expression_param);
655765

656766
std::cerr << "=== Number of failed subtests: " << num_failed << std::endl;
657767
return num_failed;

0 commit comments

Comments
 (0)