quil-lang / quilc

The optimizing Quil compiler.
Apache License 2.0
460 stars 72 forks source link

Too many SWAP instructions with NxQuantumProcessor #742

Closed daniel-mills-cqc closed 2 years ago

daniel-mills-cqc commented 3 years ago

The following code snippet:

import networkx as nx
from pyquil.quantum_processor import NxQuantumProcessor
from pyquil.api import QVMCompiler
from pyquil import Program
from pyquil.gates import CZ

coupling_map = [[0, 1], [0, 14], [1, 2], [1, 13], [2, 3], [2, 12], [3, 4], [3, 11], [4, 5], [4, 10], [5, 6], [5, 9], [6, 8], [7, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14]]

G = nx.from_edgelist(coupling_map)

quantum_processor = NxQuantumProcessor(topology=G)
compiler = QVMCompiler(quantum_processor=quantum_processor, timeout=600)

quil_circ = Program()
n_qubits = 15
for i in range(n_qubits):
    quil_circ += CZ(i%n_qubits,(i+1)%n_qubits)

compiler.quil_to_native_quil(quil_circ)

gives the error:

RPCError: Unhandled error in host program:
Too many SWAP instructions selected in a row: 1000

This is the case with the following versions:

This error does not occur if the CZs do not form a 'closed chain' . For example replacing the for loop adding the CZ gates with:

for i in range(n_qubits - 1):
    quil_circ += CZ(i%n_qubits,(i+1)%n_qubits)

does not give an error. I cannot, however, confirm that closed chains are the problem.

karlosz commented 3 years ago

The following Lisp code runs fine. I'm not totally sure this is logically equivalent to what the Python is doing, though.

(in-package :cl-quil)

(defun test ()
  (let ((chip
          (build-chip-from-digraph '((0 1) (0 14) (1 2) (1 13) (2 3) (2 12) (3 4) (3 11) (4 5) (4 10) (5 6) (5 9) (6 8) (7 8) (8 9) (9 10) (10 11) (11 12) (12 13) (13 14)) :architecture :cz))
        (n-qubits 15)
        (gates ()))
    (dotimes (i n-qubits)
      (push (build-gate "CZ" () (mod i n-qubits) (mod (1+ i) n-qubits))
            gates))
    (compiler-hook (make-instance 'parsed-program :executable-code (coerce (nreverse gates) 'vector))
                   chip)))

(test)
karlosz commented 2 years ago

Closing since issue cannot be reproduced using only quilc proper.