rigetti / qcs-sdk-rust

Rust SDK for Rigetti Quantum Cloud Services (QCS)
https://docs.rs/qcs
13 stars 5 forks source link

Unexpected RegisterMatrixConversionError when executing an 8qubit circuit on Ankaa-2 #501

Closed eginez closed 1 week ago

eginez commented 1 week ago

Hey all I am witnessing a puzzling error when executing the following circuit:

The below code can be used to trigget the problem

def _test_rigetti_compiler(device_name: str) -> None:
    program_src = """DECLARE c BIT[7]
RX(pi) 7
H 0
H 1
H 2
H 3
H 4
H 5
H 6
H 7
CNOT 0 7
CNOT 1 7
CNOT 2 7
CNOT 3 7
CNOT 4 7
CNOT 5 7
CNOT 6 7
H 0
H 1
H 2
H 3
H 4
H 5
H 6
H 7
MEASURE 0 c[0]
MEASURE 1 c[1]
MEASURE 2 c[2]
MEASURE 3 c[3]
MEASURE 4 c[4]
MEASURE 5 c[5]
MEASURE 6 c[6]"""

    program_src_4 = """DECLARE flattened BIT[3]
 RX(pi) 3
 H 0
 H 1
 H 2
 H 3
 CNOT 0 3
 CNOT 1 3
 CNOT 2 3
 H 0
 H 1
 H 2
 H 3
 MEASURE 0 flattened[0]
 MEASURE 1 flattened[1]
 MEASURE 2 flattened[2]"""

    quantum_computer = get_qc(device_name)
    builder = ExecutionOptionsBuilder.default()
    builder.connection_strategy = ConnectionStrategy.direct_access()
    options = builder.build()
    program = Program(program_src)
    register_name = next(iter(program.declarations))
    native_quil = quantum_computer.compiler.quil_to_native_quil(program)
    logger.info("Native quil: %s", str(native_quil))
    program_with_shots = native_quil.wrap_in_numshots_loop(10)
    executable = quantum_computer.compiler.native_quil_to_executable(program_with_shots)
    with rigetti_proxy_url():
        result: QAMExecutionResult = quantum_computer.run(
            executable, execution_options=options
        )
        logger.info("register name: %s", register_name)
        logger.info("exec data: %s", result.data)
        logger.info("mem vals: %s", result.get_raw_readout_data().memory_values)
        logger.info("exec ro sources: %s", result.executable.ro_sources)
        logger.info("raw readout %s", result.get_raw_readout_data())
        logger.info("readout values: %s", result.get_raw_readout_data().readout_values)
        raw_data: RawQPUReadoutData = result.get_raw_readout_data()
        bitstrings  = result.get_register_map().get(register_name, None)
    assert bitstrings is not None

The output of this script is:

2024-09-23 16:37:14,281 - __main__ - INFO - Native quil: DECLARE c BIT[7]
RZ(pi/2) 6
RX(pi/2) 6
RZ(pi) 6
RZ((-3*pi)/2) 5
RX(pi/2) 5
RZ(pi/2) 5
ISWAP 6 5
RZ((-3*pi)/2) 6
RX(pi/2) 6
RZ((3*pi)/2) 6
ISWAP 6 5
RZ(-pi) 5
RX(pi/2) 5
RZ(pi/2) 6
RX(pi/2) 6
RZ(pi/2) 6
MEASURE 6 c[0]
RZ(pi/2) 58
RX(pi/2) 58
RZ(pi/2) 58
ISWAP 51 58
RX(pi/2) 58
ISWAP 51 58
RX(pi/2) 51
ISWAP 51 58
RZ(pi/2) 12
RX(pi/2) 12
RZ(pi) 12
ISWAP 12 5
RZ((-3*pi)/2) 12
RX(pi/2) 12
RZ((3*pi)/2) 12
ISWAP 12 5
RZ(-pi) 5
RX(pi/2) 5
RZ(pi/2) 12
RX(pi/2) 12
RZ(pi/2) 12
MEASURE 12 c[1]
ISWAP 44 51
RX(pi/2) 51
ISWAP 44 51
RX(pi/2) 44
ISWAP 44 51
RX(pi/2) 37
RZ(-pi/2) 44
RX(pi/2) 44
RZ(pi/2) 44
CZ 37 44
RZ(pi/2) 44
RX(pi/2) 44
RZ(pi/2) 44
RZ((-3*pi)/2) 37
RX(pi/2) 37
RZ((3*pi)/2) 37
CZ 44 37
RZ(pi/2) 37
RX(pi/2) 37
RZ(pi/2) 37
RZ((-3*pi)/2) 44
RX(pi/2) 44
RZ((3*pi)/2) 44
CZ 37 44
RZ(-2.228093422063399) 37
RX(pi/2) 37
RZ(pi) 37
RX(-pi/2) 37
RZ(-2.2280934220634) 37
ISWAP 30 37
RX(pi/2) 37
ISWAP 30 37
RX(pi/2) 30
ISWAP 30 37
RZ(-pi/2) 31
RX(pi/2) 31
RZ((3*pi)/2) 31
RZ(pi/2) 30
RX(pi/2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5
RZ(pi/2) 12
RX(pi/2) 12
RZ(pi/2) 12
RX(pi/2) 13
MEASURE 12 c[4]
RZ(pi/2) 3
RX(pi/2) 3
RZ(pi/2) 3
ISWAP 3 4
RX(pi/2) 4
ISWAP 3 4
RX(pi/2) 3
ISWAP 3 4
RX(pi/2) 4
RZ(pi/2) 4
ISWAP 4 5
RZ((-3*pi)/2) 4
RX(pi/2) 4
RZ((3*pi)/2) 4
ISWAP 4 5
RZ(-pi) 5
RX(pi/2) 5
RZ(pi/2) 4
RX(pi/2) 4
RZ(pi/2) 4
MEASURE 4 c[5]
ISWAP 18 25
RX(pi/2) 25
ISWAP 18 25
RX(pi/2) 18
ISWAP 18 25
ISWAP 18 19
RX(pi/2) 19
ISWAP 18 19
RX(pi/2) 18
ISWAP 18 19
ISWAP 12 13
RX(pi/2) 13
ISWAP 12 13
RX(pi/2) 12
ISWAP 12 13
RX(pi/2) 19
ISWAP 12 19
RX(pi/2) 19
ISWAP 12 19
RX(pi/2) 12
ISWAP 12 19
RZ(pi/2) 12
ISWAP 12 5
RZ((-3*pi)/2) 12
RX(pi/2) 12
RZ((3*pi)/2) 12
ISWAP 12 5
RZ(pi/2) 12
RX(pi/2) 12
RZ(pi/2) 12
RZ(pi/2) 5
RX(pi/2) 5
RX(pi/2) 19
MEASURE 12 c[6]
RX(pi/2) 13
RX(pi/2) 25
RX(pi/2) 31
RZ(pi) 31
RX(-pi/2) 31
RZ(-pi) 31
RX(pi/2) 37
RX(pi/2) 58
RX(pi/2) 51
RX(pi/2) 44
RZ(pi/2) 44
RX(pi/2) 30
RZ(pi) 30
RX(-pi/2) 30
RZ(-pi) 30
RX(pi/2) 32
RX(pi/2) 20
HALT

2024-09-23 16:37:14,282 - tracing.span - INFO - translate;
2024-09-23 16:37:14,926 - qcs.qpu.api - INFO - get_default_endpoint cache miss quantum_processor_id=Ankaa-2
2024-09-23 16:37:16,097 - __main__ - INFO - register name: c
2024-09-23 16:37:16,097 - __main__ - INFO - exec data: ExecutionData { result_data: Qpu(QpuResultData { mappings: {"c[3]": "q4_classified", "c[6]": "q12_classified", "c[2]": "q12_classified", "c[1]": "q12_classified", "c[5]": "q4_classified", "c[4]": "q12_classified", "c[0]": "q6_classified"}, readout_values: {"q4_classified": Integer([0, 0]), "q4_unclassified": Complex([Complex { re: -0.11571072041988373, im: -0.24762192368507385 }, Complex { re: 0.46026137471199036, im: -0.5431233644485474 }]), "q6_unclassified": Complex([Complex { re: 1.898228645324707, im: -0.7195163369178772 }]), "q12_unclassified": Complex([Complex { re: 1.0172979831695557, im: -0.8337488770484924 }, Complex { re: 1.075590968132019, im: -0.7666645646095276 }, Complex { re: -1.0299341678619385, im: -0.8161014914512634 }, Complex { re: 1.3054935932159424, im: -1.1415420770645142 }]), "q6_classified": Integer([1]), "q12_classified": Integer([1, 1, 0, 1])}, memory_values: {} }), duration: Some(3.329ms) }
2024-09-23 16:37:16,097 - __main__ - INFO - mem vals: {}
2024-09-23 16:37:16,098 - __main__ - INFO - exec ro sources: {<MRef c[1]>: 'q12_classified', <MRef q4_unclassified[0]>: 'q4_unclassified', <MRef c[3]>: 'q4_classified', <MRef q12_unclassified[0]>: 'q12_unclassified', <MRef c[6]>: 'q12_classified', <MRef q6_unclassified[0]>: 'q6_unclassified', <MRef c[5]>: 'q4_classified', <MRef c[4]>: 'q12_classified', <MRef c[0]>: 'q6_classified', <MRef c[2]>: 'q12_classified'}
2024-09-23 16:37:16,098 - __main__ - INFO - raw readout <builtins.RawQPUReadoutData object at 0x7a4032225580>
2024-09-23 16:37:16,098 - __main__ - INFO - readout values: {'q4_unclassified': [(-0.11571072041988373-0.24762192368507385j), (0.46026137471199036-0.5431233644485474j)], 'q6_unclassified': [(1.898228645324707-0.7195163369178772j)], 'q12_unclassified': [(1.0172979831695557-0.8337488770484924j), (1.075590968132019-0.7666645646095276j), (-1.0299341678619385-0.8161014914512634j), (1.3054935932159424-1.1415420770645142j)], 'q12_classified': [1, 1, 0, 1], 'q4_classified': [0, 0], 'q6_classified': [1]}
Traceback (most recent call last):
  File "/home/ssm-user/.pex/venvs/ddce1dd71d7cfa3b034cd437d13cde28503bf1d5/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/pex", line 320, in <module>
    runpy.run_module(module_name, run_name="__main__", alter_sys=True)
  File "<frozen runpy>", line 226, in run_module
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/home/ssm-user/.pex/venvs/ddce1dd71d7cfa3b034cd437d13cde28503bf1d5/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.11/site-packages/embeddedcorerigetti/benchmarks/run_rigetti_embedded_benchmarks.py", line 586, in <module>
    main()
  File "/home/ssm-user/.pex/venvs/ddce1dd71d7cfa3b034cd437d13cde28503bf1d5/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.11/site-packages/embeddedcorerigetti/benchmarks/run_rigetti_embedded_benchmarks.py", line 566, in main
    _test_rigetti_compiler(args.backend_name)
  File "/home/ssm-user/.pex/venvs/ddce1dd71d7cfa3b034cd437d13cde28503bf1d5/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.11/site-packages/embeddedcorerigetti/benchmarks/run_rigetti_embedded_benchmarks.py", line 549, in _test_rigetti_compiler
    bitstrings  = result.get_register_map().get(register_name, None)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ssm-user/.pex/venvs/ddce1dd71d7cfa3b034cd437d13cde28503bf1d5/779eb2cc0ca9e2fdd204774cbc41848e4e7c5055/lib/python3.11/site-packages/pyquil/api/_qam.py", line 74, in get_register_map
    register_map = self.data.result_data.to_register_map()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
execution_data.RegisterMatrixConversionError: The data for register c does fit into a rectangular matrix

Couple of things to note

  1. This works fine on 4 qubits
  2. Inspecting the raw readout values I see different size arrays, not sure if that's correct.
eginez commented 1 week ago

For the record I tried adding the shots loop before the native quil compilation with the exact same results

eginez commented 1 week ago

Version of the compiler

2024-09-23 16:59:52,508 - __main__ - INFO - compiler version: {'quilc': '1.26.0'}
eginez commented 1 week ago

Here is the diagnostics information

platform: linux
qcs-sdk-rust version: 0.23.2
rustc version: rustc 1.80.1 (3f5fd8dd4 2024-08-06)
features: TRACING, TRACING_CONFIG, TRACING_OPENTELEMETRY
qcs:
  address: https://api.qcs.rigetti.com
  network reachable: true
quilc:
  address: tcp://127.0.0.1:5555
  version: 1.26.0
  available: true
qvm:
  address: http://127.0.0.1:5000
  version: -
  available: false
libquil:
  available: false
  quilc version: -
  qvm version: -
eginez commented 1 week ago

Ok this was resolved by running the compiler with the -P flag like so

docker run -d -p 5555:5555 rigetti/quilc -R -P