Skip to content
Open
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
40 changes: 23 additions & 17 deletions test/c/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void print_circuit(const QkCircuit *qc) {

bool compare_circuits(const QkCircuit *res, const QkCircuit *expected) {
if (qk_circuit_num_instructions(res) != qk_circuit_num_instructions(expected)) {
printf("Number of instructions in circuit is mismatched\n");
fprintf(stderr, "Number of instructions in circuit is mismatched\n");
return false;
}
QkCircuitInstruction res_inst;
Expand All @@ -74,59 +74,65 @@ bool compare_circuits(const QkCircuit *res, const QkCircuit *expected) {
qk_circuit_get_instruction(expected, i, &expected_inst);
int result = strcmp(res_inst.name, expected_inst.name);
if (result != 0) {
printf("Gate %zu have different gates %s was found and expected %s\n", i, res_inst.name,
expected_inst.name);
fprintf(stderr, "Gate %zu have different gates %s was found and expected %s\n", i,
res_inst.name, expected_inst.name);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
}
if (res_inst.num_qubits != expected_inst.num_qubits) {
printf("Gate %zu have different number of qubits %d was found and expected %d\n", i,
res_inst.num_qubits, expected_inst.num_qubits);
fprintf(stderr,
"Gate %zu have different number of qubits %d was found and expected %d\n", i,
res_inst.num_qubits, expected_inst.num_qubits);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
}
for (uint32_t j = 0; j < res_inst.num_qubits; j++) {
if (res_inst.qubits[j] != expected_inst.qubits[j]) {
printf("Qubit %d for gate %zu are different %d was found and expected %d\n", j, i,
res_inst.qubits[j], expected_inst.qubits[j]);
printf("Expected circuit instructions:\n");
fprintf(stderr,
"Qubit %d for gate %zu are different %d was found and expected %d\n", j, i,
res_inst.qubits[j], expected_inst.qubits[j]);
fprintf(stderr, "Expected circuit instructions:\n");
print_circuit(expected);
printf("Result circuit:\n");
fprintf(stderr, "Result circuit:\n");
print_circuit(res);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
}
}
if (res_inst.num_clbits != expected_inst.num_clbits) {
printf("Gate %zu have different number of clbits %d was found and expected %d\n", i,
res_inst.num_clbits, expected_inst.num_clbits);
fprintf(stderr,
"Gate %zu have different number of clbits %d was found and expected %d\n", i,
res_inst.num_clbits, expected_inst.num_clbits);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
}
for (uint32_t j = 0; j < res_inst.num_clbits; j++) {
if (res_inst.clbits[j] != expected_inst.clbits[j]) {
printf("Clbit %d for gate %zu are different %d was found and expected %d\n", j, i,
res_inst.clbits[j], expected_inst.clbits[j]);
fprintf(stderr,
"Clbit %d for gate %zu are different %d was found and expected %d\n", j, i,
res_inst.clbits[j], expected_inst.clbits[j]);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
}
}
if (res_inst.num_params != expected_inst.num_params) {
printf("Gate %zu have different number of params %d was found and expected %d\n", i,
res_inst.num_params, expected_inst.num_params);
fprintf(stderr,
"Gate %zu have different number of params %d was found and expected %d\n", i,
res_inst.num_params, expected_inst.num_params);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
}
for (uint32_t j = 0; j < res_inst.num_params; j++) {
if (res_inst.params[j] != expected_inst.params[j]) {
printf("Parameter %d for gate %zu are different %f was found and expected %f\n", j,
i, res_inst.params[j], expected_inst.params[j]);
fprintf(stderr,
"Parameter %d for gate %zu are different %f was found and expected %f\n", j,
i, res_inst.params[j], expected_inst.params[j]);
qk_circuit_instruction_clear(&res_inst);
qk_circuit_instruction_clear(&expected_inst);
return false;
Expand Down
22 changes: 14 additions & 8 deletions test/c/test_basis_translator.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ static int test_circuit_in_basis(void) {
size_t circuit_len = qk_circuit_num_instructions(circuit);
if (circuit_len != 2) {
result = EqualityError;
printf(
fprintf(
stderr,
"The number of gates resulting from the translation is incorrect. Expected 2, got %lu",
circuit_len);
goto cleanup;
Expand All @@ -51,7 +52,8 @@ static int test_circuit_in_basis(void) {

if (strcmp(inst.name, gate_names[idx]) != 0) {
result = EqualityError;
printf(
fprintf(
stderr,
"The operation resulting from this translation was incorrect. Expected '%s' gate, "
"got '%s'",
gate_names[idx], inst.name);
Expand Down Expand Up @@ -84,7 +86,8 @@ static int test_basic_basis_translator(void) {

if (result_op_counts.len != 1) {
result = EqualityError;
printf(
fprintf(
stderr,
"The number of gates resulting from the translation is incorrect. Expected 1, got %lu",
result_op_counts.len);
goto cleanup;
Expand All @@ -93,9 +96,10 @@ static int test_basic_basis_translator(void) {
QkOpCount u_count = result_op_counts.data[0];
if (u_count.count != 1 || strcmp(u_count.name, "u") != 0) {
result = EqualityError;
printf("The operation resulting from this translation was incorrect. Expected 'u' gate, "
"got '%s'",
u_count.name);
fprintf(stderr,
"The operation resulting from this translation was incorrect. Expected 'u' gate, "
"got '%s'",
u_count.name);
}

cleanup:
Expand Down Expand Up @@ -125,7 +129,8 @@ static int test_toffoli_basis_translator(void) {

if (result_op_counts.len != 4) {
result = EqualityError;
printf(
fprintf(
stderr,
"The number of gates resulting from the translation is incorrect. Expected 1, got %lu",
result_op_counts.len);
goto cleanup;
Expand All @@ -138,7 +143,8 @@ static int test_toffoli_basis_translator(void) {
QkOpCount gate_count = result_op_counts.data[idx];
if (gate_count.count != freqs[idx] || strcmp(gate_count.name, gates[idx]) != 0) {
result = EqualityError;
printf(
fprintf(
stderr,
"The operation resulting from this translation was incorrect. Expected '%s' gate, "
"got '%s'",
gates[idx], gate_count.name);
Expand Down
42 changes: 21 additions & 21 deletions test/c/test_circuit.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ static int test_empty(void) {
qk_circuit_free(qc);

if (opcount != 0) {
printf("The operation count %zu is not 0", opcount);
fprintf(stderr, "The operation count %zu is not 0", opcount);
return EqualityError;
}
if (num_qubits != 0) {
printf("The number of qubits %d is not 0", num_qubits);
fprintf(stderr, "The number of qubits %d is not 0", num_qubits);
return EqualityError;
}
if (num_clbits != 0) {
printf("The number of clbits %d is not 0", num_clbits);
fprintf(stderr, "The number of clbits %d is not 0", num_clbits);
return EqualityError;
}
if (num_instructions != 0) {
printf("The number of instructions %zu is not 0", num_instructions);
fprintf(stderr, "The number of instructions %zu is not 0", num_instructions);
return EqualityError;
}
return Ok;
Expand All @@ -63,15 +63,15 @@ static int test_circuit_with_quantum_reg(void) {
qk_circuit_free(qc);
qk_quantum_register_free(qr);
if (num_qubits != 1024) {
printf("The number of qubits %d is not 1024", num_qubits);
fprintf(stderr, "The number of qubits %d is not 1024", num_qubits);
return EqualityError;
}
if (num_clbits != 0) {
printf("The number of clbits %d is not 0", num_clbits);
fprintf(stderr, "The number of clbits %d is not 0", num_clbits);
return EqualityError;
}
if (num_instructions != 0) {
printf("The number of instructions %zu is not 0", num_instructions);
fprintf(stderr, "The number of instructions %zu is not 0", num_instructions);
return EqualityError;
}
return Ok;
Expand All @@ -94,8 +94,8 @@ static int test_circuit_copy(void) {
qk_circuit_free(qc);
qk_circuit_free(copy);
if (num_instructions == num_copy_instructions) {
printf("The number of instructions %zu is equal to the copied %zu", num_instructions,
num_copy_instructions);
fprintf(stderr, "The number of instructions %zu is equal to the copied %zu",
num_instructions, num_copy_instructions);
return EqualityError;
}
return Ok;
Expand All @@ -111,15 +111,15 @@ static int test_circuit_with_classical_reg(void) {
qk_circuit_free(qc);
qk_classical_register_free(cr);
if (num_qubits != 0) {
printf("The number of qubits %d is not 0", num_qubits);
fprintf(stderr, "The number of qubits %d is not 0", num_qubits);
return EqualityError;
}
if (num_clbits != 2048) {
printf("The number of clbits %d is not 2048", num_clbits);
fprintf(stderr, "The number of clbits %d is not 2048", num_clbits);
return EqualityError;
}
if (num_instructions != 0) {
printf("The number of instructions %zu is not 0", num_instructions);
fprintf(stderr, "The number of instructions %zu is not 0", num_instructions);
return EqualityError;
}
return Ok;
Expand All @@ -138,8 +138,8 @@ static int test_circuit_copy_with_instructions(void) {
size_t num_instructions = qk_circuit_num_instructions(qc);
size_t num_copy_instructions = qk_circuit_num_instructions(copy);
if (num_instructions != num_copy_instructions) {
printf("The number of instructions %zu does not equal the copied %zu", num_instructions,
num_copy_instructions);
fprintf(stderr, "The number of instructions %zu does not equal the copied %zu",
num_instructions, num_copy_instructions);
return EqualityError;
}

Expand All @@ -163,8 +163,8 @@ static int test_circuit_copy_with_instructions(void) {
qk_circuit_free(qc);
qk_circuit_free(copy);
if (num_instructions == num_copy_instructions) {
printf("The number of instructions %zu is equal to the copied %zu", num_instructions,
num_copy_instructions);
fprintf(stderr, "The number of instructions %zu is equal to the copied %zu",
num_instructions, num_copy_instructions);
return EqualityError;
}
return Ok;
Expand All @@ -178,15 +178,15 @@ static int test_no_gate_1000_bits(void) {
qk_circuit_free(qc);

if (num_qubits != 1000) {
printf("The number of qubits %d is not 1000", num_qubits);
fprintf(stderr, "The number of qubits %d is not 1000", num_qubits);
return EqualityError;
}
if (num_clbits != 1000) {
printf("The number of clbits %d is not 1000", num_clbits);
fprintf(stderr, "The number of clbits %d is not 1000", num_clbits);
return EqualityError;
}
if (num_instructions != 0) {
printf("The number of instructions %zu is not 0", num_instructions);
fprintf(stderr, "The number of instructions %zu is not 0", num_instructions);
return EqualityError;
}

Expand Down Expand Up @@ -898,14 +898,14 @@ static int test_not_unitary_gate(void) {

int result = Ok;
if (exit_code != QkExitCode_ExpectedUnitary) {
printf("Got exit code %i but expected %i", exit_code, QkExitCode_ExpectedUnitary);
fprintf(stderr, "Got exit code %i but expected %i", exit_code, QkExitCode_ExpectedUnitary);
result = EqualityError;
goto cleanup;
}

size_t num_inst = qk_circuit_num_instructions(qc);
if (num_inst != 0) { // we expect no gate was added
printf("Found gate when none should be added");
fprintf(stderr, "Found gate when none should be added");
result = EqualityError;
goto cleanup;
}
Expand Down
8 changes: 4 additions & 4 deletions test/c/test_commutative_cancellation.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ static int test_commutative_cancellation_target(void) {

result = qk_transpiler_pass_standalone_commutative_cancellation(qc, target, 1.0);
if (result != 0) {
printf("Running the pass failed");
fprintf(stderr, "Running the pass failed");
goto cleanup;
}

if (qk_circuit_num_instructions(qc) != 1) {
result = EqualityError;
printf("The gates weren't removed by this circuit");
fprintf(stderr, "The gates weren't removed by this circuit");
}
cleanup:
qk_circuit_free(qc);
Expand Down Expand Up @@ -79,13 +79,13 @@ static int test_commutative_cancellation_no_target(void) {

result = qk_transpiler_pass_standalone_commutative_cancellation(qc, NULL, 1.0);
if (result != 0) {
printf("Running the pass failed");
fprintf(stderr, "Running the pass failed");
goto cleanup;
}

if (qk_circuit_num_instructions(qc) != 1) {
result = EqualityError;
printf("The gates weren't removed by this circuit");
fprintf(stderr, "The gates weren't removed by this circuit");
}
cleanup:
qk_circuit_free(qc);
Expand Down
15 changes: 9 additions & 6 deletions test/c/test_consolidate_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,22 @@ static int test_non_cx_target(void) {
QkOpCounts counts = qk_circuit_count_ops(qc);
if (counts.len != 1) {
result = EqualityError;
printf("The pass run did not result in a circuit with one unitary gate. Expected 1 gate, "
"got %li.",
counts.len);
fprintf(stderr,
"The pass run did not result in a circuit with one unitary gate. Expected 1 gate, "
"got %li.",
counts.len);
goto cleanup;
}

QkCircuitInstruction unitary;
qk_circuit_get_instruction(qc, 0, &unitary);
if (strcmp(unitary.name, "unitary") != 0) {
result = EqualityError;
printf("The pass run did not result in a circuit with one unitary gate. Expected 'unitary' "
"gate, got '%s'.",
unitary.name);
fprintf(
stderr,
"The pass run did not result in a circuit with one unitary gate. Expected 'unitary' "
"gate, got '%s'.",
unitary.name);
goto cleanup_inst;
}
cleanup_inst:
Expand Down
10 changes: 5 additions & 5 deletions test/c/test_elide_permutations.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static int test_elide_permutations_no_result(void) {
int result = Ok;
QkTranspileLayout *pass_result = qk_transpiler_pass_standalone_elide_permutations(qc);
if (pass_result != NULL) {
printf("A gate was elided when one shouldn't have been\n");
fprintf(stderr, "A gate was elided when one shouldn't have been\n");
qk_transpile_layout_free(pass_result);
result = EqualityError;
}
Expand All @@ -61,7 +61,7 @@ static int test_elide_permutations_swap_result(void) {
int result = Ok;
QkTranspileLayout *pass_result = qk_transpiler_pass_standalone_elide_permutations(qc);
if (pass_result == NULL) {
printf("A gate wasn't elided when one should have been\n");
fprintf(stderr, "A gate wasn't elided when one should have been\n");
result = EqualityError;
goto cleanup;
}
Expand All @@ -70,21 +70,21 @@ static int test_elide_permutations_swap_result(void) {
uint32_t expected_permutation[5] = {0, 3, 2, 1, 4};
for (int i = 0; i < 5; i++) {
if (permutation[i] != expected_permutation[i]) {
printf("Permutation doesn't match expected\n");
fprintf(stderr, "Permutation doesn't match expected\n");
result = EqualityError;
goto result_cleanup;
}
}
QkOpCounts op_counts = qk_circuit_count_ops(qc);
if (op_counts.len != 1) {
printf("More than 1 type of gates in circuit\n");
fprintf(stderr, "More than 1 type of gates in circuit\n");
result = EqualityError;
goto ops_cleanup;
}
for (size_t i = 0; i < op_counts.len; i++) {
int swap_gate = strcmp(op_counts.data[i].name, "swap");
if (swap_gate == 0) {
printf("Swap gate in circuit which should have been elided\n");
fprintf(stderr, "Swap gate in circuit which should have been elided\n");
result = EqualityError;
goto ops_cleanup;
}
Expand Down
Loading