Skip to content

[Verifier] Add a function to print out the difference of two circuits#228

Merged
xumingkuan merged 2 commits intomasterfrom
compare
Nov 19, 2025
Merged

[Verifier] Add a function to print out the difference of two circuits#228
xumingkuan merged 2 commits intomasterfrom
compare

Conversation

@xumingkuan
Copy link
Copy Markdown
Collaborator

This PR adds these functions:

  /**
   * A helper method to extract the difference between two circuits, used
   * by the equivalent() function above. The two circuits are expected
   * to differ by only one small circuit transformation.
   * @param ctx The context for both circuits.
   * @param circuit1 The first input circuit.
   * @param circuit2 The second input circuit.
   * @param output_circuit1 Return the different part for circuit1.
   * Should pass in a variable initialized to nullptr to store the return value.
   * @param output_circuit2 Return the different part for circuit2.
   * Should pass in a variable initialized to nullptr to store the return value.
   * @param verbose Print logs to the screen.
   * @return True iff the function succeeded.
   */
  static bool extract_difference(Context *ctx, const CircuitSeq *circuit1,
                                 const CircuitSeq *circuit2,
                                 std::unique_ptr<CircuitSeq> &output_circuit1,
                                 std::unique_ptr<CircuitSeq> &output_circuit2,
                                 bool verbose = false);
  /**
   * Extract the difference between two circuits to a string.
   * @param ctx The context for both circuits.
   * @param circuit1 The first input circuit.
   * @param circuit2 The second input circuit.
   * @param columns_before_midline Print out a character '|' at this location
   * for each line to separate two circuits.
   * @param param_precision The parameter precision for the output.
   * @return A string side-by-side for the difference.
   */
  static std::string difference_str(Context *ctx, const CircuitSeq *circuit1,
                                    const CircuitSeq *circuit2,
                                    int columns_before_midline = 40,
                                    int param_precision = kDefaultQASMParamPrecision);
  static std::string difference_str(const Graph *circuit1,
                                    const Graph *circuit2,
                                    int columns_before_midline = 40,
                                    int param_precision = kDefaultQASMParamPrecision);

Usage example:

#include "quartz/verifier/verifier.h"
int main() {
  ParamInfo param_info;
  Context ctx({GateType::rz, GateType::input_qubit, GateType::input_param,
               GateType::mult, GateType::pi},
              1, &param_info);

  std::string str = "OPENQASM 2.0;\n"
                    "include \"qelib1.inc\";\n"
                    "qreg q[1];\n"
                    "rz(pi*1/2) q[0];\n"
                    "rz(pi*2/3) q[0];\n"
                    "rz(pi) q[0];\n"
                    "rz(pi/4) q[0];\n";
  auto graph1 = Graph::from_qasm_str(&ctx, str);
  str = "OPENQASM 2.0;\n"
        "include \"qelib1.inc\";\n"
        "qreg q[1];\n"
        "rz(pi/2) q[0];\n"
        "rz(2*pi/3) q[0];\n"
        "rz(pi/2) q[0];\n"
        "rz(pi/4) q[0];\n";
  auto graph2 = Graph::from_qasm_str(&ctx, str);
  std::cout << Verifier::difference_str(graph1.get(), graph2.get());
  return 0;
}

Output:

rz(pi*1) q[0];                          | rz(pi*1/2) q[0];

@xumingkuan xumingkuan merged commit ba4f439 into master Nov 19, 2025
2 checks passed
@xumingkuan xumingkuan deleted the compare branch November 19, 2025 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant