Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler outputs different programs non-deterministically #676

Open
mhodson-rigetti opened this issue Oct 15, 2020 · 6 comments
Open

Compiler outputs different programs non-deterministically #676

mhodson-rigetti opened this issue Oct 15, 2020 · 6 comments
Assignees
Labels
bug Something isn't working tech-debt Technical debt because of incomplete, lazy, shortsighted, etc. software engineering.

Comments

@mhodson-rigetti
Copy link

mhodson-rigetti commented Oct 15, 2020

Compiling the following bell-state program via pyquil on "9q-square-qvm":

    program = Program()
    ro = program.declare("ro", "BIT", 2)
    program.inst(H(1))
    program.inst(CNOT(1, 4))
    program.inst(MEASURE(1, ro[0]))
    program.inst(MEASURE(4, ro[1]))

... using the following quilc ISA patched to remove CZ gates (force use of XY)

{'1Q': {'1': {'gates': [{'operator': 'I', 'parameters': [], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [0], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [1.5707963267948966], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [-1.5707963267948966], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [3.141592653589793], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [-3.141592653589793], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RZ', 'parameters': ['theta'], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'MEASURE', 'qubit': '_', 'target': '_', 'duration': None, 'fidelity': None}, {'operator': 'MEASURE', 'qubit': '_', 'target': None, 'duration': None, 'fidelity': None}]}, '4': {'gates': [{'operator': 'I', 'parameters': [], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [0], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [1.5707963267948966], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [-1.5707963267948966], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [3.141592653589793], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RX', 'parameters': [-3.141592653589793], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'RZ', 'parameters': ['theta'], 'arguments': ['_'], 'fidelity': None, 'duration': None}, {'operator': 'MEASURE', 'qubit': '_', 'target': '_', 'duration': None, 'fidelity': None}, {'operator': 'MEASURE', 'qubit': '_', 'target': None, 'duration': None, 'fidelity': None}]}}, '2Q': {'1-4': {'gates': [{'operator': 'XY', 'parameters': ['theta'], 'arguments': ['_', '_'], 'fidelity': None, 'duration': None}]}}}

... generates two different native programs with seemingly non-deterministic behavior:

DECLARE ro BIT[2]
RZ(-pi/2) 1
RX(-pi/2) 1
RZ(pi) 4
XY(pi) 1 4
RZ(pi/2) 1
RX(pi/2) 1
RZ(-pi/2) 1
XY(pi) 1 4
RX(-pi/2) 4
MEASURE 4 ro[1]
MEASURE 1 ro[0]

(most of the time)

DECLARE ro BIT[2]
RZ(pi/2) 1
RX(pi/2) 1
RZ(pi) 4
RX(-pi/2) 4
XY(pi) 1 4
RZ(-pi/2) 1
RX(pi/2) 1
RZ(pi/2) 1
XY(pi) 1 4
MEASURE 4 ro[1]
MEASURE 1 ro[0]

(occasionally)

@colescott
Copy link
Member

Are you able to upload the verbose output of quilc for both outputs to see what the compiler is up to?

@notmgsk notmgsk self-assigned this Oct 15, 2020
@mimigdal
Copy link

mimigdal commented Oct 25, 2021

I have a similar problem. See attached script. Sometimes I get the answer reported in the last cell and sometimes the answer in the one before the last cell. I couldn't identify any pattern of when I get one answer or another. I noticed that if I use "device_name=40q-qvm" instead of "device_name=Aspen-9-qvm", the program is compiled differently (i.e. CZ gate is used instead of CPHASE(pi)) and there's no problem. Interestingly, both the program reported here and the one I used involve the XY gate.

compiler_question.zip

@stylewarning
Copy link
Member

I've noticed this non-determinism more as-of-late as well, and it's irksome. I sometimes have to run the compiler 2-3 times to find the "best" one of the bunch, and I'm not yet sure of the source of the issue.

@stylewarning stylewarning added bug Something isn't working tech-debt Technical debt because of incomplete, lazy, shortsighted, etc. software engineering. labels Oct 25, 2021
@stylewarning
Copy link
Member

I labeled this as a bug due to its woeful disregard of expectations, even though it may not be "wrong".

@mimigdal It's almost surely due to some routines in the compiler which depend on non-determinism. Those are being looked into now.

@mimigdal
Copy link

mimigdal commented Oct 25, 2021 via email

@stylewarning stylewarning pinned this issue Oct 25, 2021
@stylewarning stylewarning changed the title Inconsistent results compiling bell state program Compiler outputs different programs non-deterministically Oct 25, 2021
@stylewarning
Copy link
Member

CC @macrologist

macrologist added a commit to macrologist/quilc that referenced this issue Oct 18, 2023
This partially addresses one source of "nondeterminism" in quilc
compilation (see quil-lang#676) that was due to a violation of referential
transparency.

Specifically, non destructive compilation was not non-destructive
enough: compiling the same parsed-program instance twice did not
successfuly copy measurement instances deeply, resulting in different
outputs from compress-qubits, with downstream consequences.
stylewarning pushed a commit that referenced this issue Oct 19, 2023
This partially addresses one source of "nondeterminism" in quilc
compilation (see #676) that was due to a violation of referential
transparency.

Specifically, non destructive compilation was not non-destructive
enough: compiling the same parsed-program instance twice did not
successfuly copy measurement instances deeply, resulting in different
outputs from compress-qubits, with downstream consequences.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tech-debt Technical debt because of incomplete, lazy, shortsighted, etc. software engineering.
Projects
None yet
Development

No branches or pull requests

5 participants