PECOS-packages / PECOS

A Python framework, library, and set of tools for exploring, developing, and evaluating quantum error-correction protocols.
Other
29 stars 5 forks source link

`KeyError: 'H4'` with certain parameter values #86

Open cqc-alec opened 1 week ago

cqc-alec commented 1 week ago

Version: quantum-pecos 0.6.0.dev5

from pecos.engines.hybrid_engine import HybridEngine

phir = """
{
    "format": "PHIR/JSON",
    "version": "0.1.0",
    "metadata":
    {
        "source": "pytket-phir v0.8.1"
    },
    "ops":
    [
        {
            "data": "qvar_define",
            "data_type": "qubits",
            "variable": "q",
            "size": 1
        },
        {
            "qop": "R1XY",
            "angles":
            [
                [
                    1.0,
                    1.75
                ],
                "pi"
            ],
            "args":
            [
                [
                    "q",
                    0
                ]
            ]
        }
    ]
}
"""

engine = HybridEngine(qsim="state-vector")
engine.run(phir, shots=10)

Output:

Traceback (most recent call last):
  File "/home/alec/r/pytket-quantinuum/play/john.py", line 51, in <module>
    r = engine.run(phir, shots=10)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/pecos/engines/hybrid_engine.py", line 191, in run
    temp_meas = self.qsim.run(noisy_qops)
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/pecos/simulators/quantum_simulator.py", line 95, in run
    output = self.state.run_gate(op.sim_name, op.args, **op.metadata)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/pecos/simulators/parent_sim_classes.py", line 43, in run_gate
    results = self.bindings[symbol](self, location, **params)
              ~~~~~~~~~~~~~^^^^^^^^
KeyError: 'H4'

If "1.75" is changed to "1.76", there is no error!

cqc-alec commented 1 week ago

The bug does not appear with quantum-pecos 0.6.0.dev3: it seems to have been introduced in quantum-pecos 0.6.0.dev4.

PabloAndresCQ commented 1 week ago

This is what appears to be happening:

I do not know what's the code responsible for the conversion of R1XY to H4. However, a simple fix for this is to add H4 and all other Clifford gates in the list here to the simulators. I will do this in a PR + include a better error message when a gate is not supported by a simulator.

PabloAndresCQ commented 1 week ago

It is likely that the conversion from R1XY to H4 has been a feature of PECOS since before .dev4. The bug is likely to have appeared on .dev4 onwards due to a change of the default simulator from ProjectQ to Qulacs. The wrapper of ProjectQ had support for the H4 gate.

PabloAndresCQ commented 1 week ago

In theory, a workaround for users is to select ProjectQ as the simulator (the one that was used in .dev3). Unfortunately, I'm getting a different error which may be related to my ProjectQ installation. Does this work for anyone else?

engine = HybridEngine(qsim="ProjectQSim")

Another option is to use the version of PECOS from PR #87.

cqc-alec commented 1 week ago

When I run with dev3 I do get the following scary output:

Exception ignored in: <function ProjectQSim.__del__ at 0x77dadad91300>
Traceback (most recent call last):
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/pecos/simulators/projectq/state.py", line 155, in __del__
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/projectq/cengines/_main.py", line 326, in flush
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/projectq/ops/_command.py", line 118, in __init__
  File "/home/alec/r/pytket-quantinuum/env/lib/python3.12/site-packages/projectq/ops/_command.py", line 248, in control_state
ModuleNotFoundError: import of projectq.meta halted; None in sys.modules

However, I think this is harmless -- something to do with ProjectQ not being shut down correctly on program exit.

ciaranra commented 1 week ago

Yeah, it would be good to figure out the right way to approach turning on an off the general gate to Clifford conversion. But that might take some thinking out to do. Pablo's fix in PR #87 seems like a good solution for now.

ciaranra commented 1 week ago

The error with ProjectQ is a bit concerning. Although, it can be a bit frustrating to set up. @cqc-alec what operating system are you using?

ciaranra commented 1 week ago

Although, reading this again... I seem to recall that you had a similar issue in the past when using Python's REPL and not in other circumstances, which was never solved.

cqc-alec commented 6 days ago

Yes I did get a similar issue in the past. I'm using Linux. It's not just using the REPL; I get it when exexuting python my_script.py from bash.

cqc-alec commented 5 days ago

@ciaranra Can we have a new dev release so that we can close this?