quantum-compiler / quartz

The Quartz Quantum Compiler
Apache License 2.0
76 stars 19 forks source link

Questions for transformation generation #179

Open IsolatedMy opened 1 month ago

IsolatedMy commented 1 month ago

Hi. I wonder what the information in each file stands for. For example, I generate transformations for "Nam_2_3", and there are 4 files generated.

If I only need valid transformations, which can optimize quantum gate count, does it mean I only need to read XX_complete_ECC_set.json file? Besides, I wonder what the decimals in the header of Nam_2_3_complete_ECC_set.json file used for.

[
[[6, "", "", ["add", ["P2"], ["P0", "P0"]], ["add", ["P3"], ["P0", "P1"]], ["add", ["P4"], ["P1", "P1"]], ["add", ["P5"], ["P1", "P0"]]], [2, 5.83359930348343436e-01, 2.16308546607830365e+00]],
{

Thanks.

xumingkuan commented 1 month ago

If I only need valid transformations, which can optimize quantum gate count, does it mean I only need to read XX_complete_ECC_set.json file?

Correct.

Besides, I wonder what the decimals in the header of Nam_2_3_complete_ECC_set.json file used for.

The decimals are generated from here:

https://github.com/quantum-compiler/quartz/blob/66c8d23dfb7efe1aa0991c7c4a524373b42717d3/src/quartz/context/context.cpp#L335

They specify the parameter expressions used in the ECC set. For example, ["add", ["P2"], ["P0", "P0"]] means that the parameter P2 is equal to 2 * P0. If this is removed, Quartz will treat P2 as an input parameter instead of an expression.

The decimal numbers at the end are the random floating point values used for generating the fingerprint (hash value) of the circuits. This is recorded to avoid different behavior if the hash value is recalculated each time we read an ECC set.

IsolatedMy commented 1 month ago

Thanks for your patience. I still have some questions.

xumingkuan commented 1 month ago
  • For [["rz", ["Q1"], ["Q1", "P0"]], I think ["Q1"] represents the operator qubit. But I wonder what ["Q1", "P0"] stands for.

P0 is the parameter input to the rz gate, and P0 represents the 0-th parameter (both qubits and parameters are 0-indexed). You can view this as Q1 := rz(Q1, P0) where := means assignment in this pseudocode.

  • And for ["cx", ["Q1", "Q0"], ["Q1", "Q0"]], I wonder why ["Q1", "Q0"] appear twice.

Similar to [Q1, Q0] := cx(Q1, Q0), the first ["Q1", "Q0"] is the output, and the second ["Q1", "Q0"] is the input. This design is for potential extension of gates that do not preserve the qubit order. For example, if you swap Q0 and Q1 physically without using a SWAP gate during the cx operation, you can write ["cx", ["Q0", "Q1"], ["Q1", "Q0"]].

IsolatedMy commented 3 weeks ago

Hi. I notice in the given H_CZ_2_2_complete_ECC_set_modified.json, it seems that some transformations are actually unnecessary. For example, we can use a symbolic qubit parameter to generalize the first two transformations into one [[2,2], [["h", ["Q"], ["Q"]], ["h", ["Q"], ["Q"]]]. I wonder if you have thought this.

[
  [[0], [0]],
  {
    "0_5": [
      [[2,0], []]
      ,[[2,2], [["h", ["Q0"], ["Q0"]], ["h", ["Q0"], ["Q0"]]]]
      ,[[2,2], [["h", ["Q1"], ["Q1"]], ["h", ["Q1"], ["Q1"]]]]
      ,[[2,2], [["cz", ["Q0", "Q1"], ["Q0", "Q1"]], ["cz", ["Q0", "Q1"], ["Q0", "Q1"]]]]
      ,[[2,2], [["cz", ["Q0", "Q1"], ["Q0", "Q1"]], ["cz", ["Q1", "Q0"], ["Q1", "Q0"]]]]
    ]
  }
]
xumingkuan commented 3 weeks ago

Yeah, simplifying the ECC Sets is something on my TODO list for years but I've never got the time to do it.

For this example, because all qubits in ECC Sets are actually symbolic, you can actually only keep [[2,2], [["h", ["Q0"], ["Q0"]], ["h", ["Q0"], ["Q0"]]]] (removing [[2,2], [["h", ["Q1"], ["Q1"]], ["h", ["Q1"], ["Q1"]]]]) and it should not affect the result. If, for example, we need to replace ["cz", ["Q0", "Q1"], ["Q0", "Q1"]], ["cz", ["Q0", "Q1"], ["Q0", "Q1"]] with ["h", ["Q1"], ["Q1"]], ["h", ["Q1"], ["Q1"]] in a circuit, we can replace it with [] first, and then do a pattern matching with a different qubit mapping where the symbolic Q0 points to Q1, and then do the replacement to ["h", ["Q1"], ["Q1"]], ["h", ["Q1"], ["Q1"]].

If you want to start a research project to do the simplification more systematically, we can chat through email. My email is mingkuan@cmu.edu.