Closed chriseclectic closed 1 year ago
Sounds like at a minimum we need a bunch more equivalence-library rules, and longer term, we need to expand the Clifford-specific synthesis routines (@ShellyGarion probably knows the state of that work).
As an immediate workaround, you can do this at the start of your Python session:
import math
from qiskit.circuit.equivalence_library import SessionEquivalenceLibrary
from qiskit.circuit import library as lib, QuantumCircuit
cx_to_ecr = QuantumCircuit(2, global_phase=math.pi / 4)
cx_to_ecr.sxdg(1)
cx_to_ecr.sdg(0)
cx_to_ecr.ecr(0, 1)
cx_to_ecr.x(0)
SessionEquivalenceLibrary.add_equivalence(lib.CXGate(), cx_to_ecr)
h_to_sx = QuantumCircuit(1, global_phase=-math.pi/4)
h_to_sx.s(0)
h_to_sx.sx(0)
h_to_sx.s(0)
SessionEquivalenceLibrary.add_equivalence(lib.HGate(), h_to_sx)
and then all of your lines should work. They won't be the most efficient synthesis (almost certainly), but they'll at least compile.
(It's also probably worth checking that I got those equivalences correct lol)
They won't be the most efficient synthesis
print(transpile(qc, basis_gates=["ecr", "s", "sx"]))
global phase: 0
┌───┐┌───┐ ┌────┐┌───┐┌───┐┌───┐ ┌────┐┌───┐ ┌───┐ ┌──────┐┌───┐ ┌───┐ ┌───┐ ┌──────┐┌───┐ ┌────┐┌───┐ ┌───┐ ┌───┐┌───┐ ┌────┐┌───┐ ┌───┐┌───┐┌────┐┌───┐┌───┐┌───┐┌───┐┌──────┐┌───┐┌────┐┌───┐ ┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐┌───┐┌────┐»
q_0: ┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤ S ├─┤ √X ├┤ S ├─┤ S ├───────┤1 ├┤ S ├─┤ S ├──┤ S ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤0 ├┤ S ├─┤ √X ├┤ S ├─┤ S ├──┤ S ├┤ S ├─┤ √X ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ √X ├»
├───┤├───┴┐├───┬┘├───┤├───┤├───┴┐├───┬┘├───┤ └───┘ │ │└───┘ └───┘ ┌┴───┴─┐┌───┐┌────┐┌───┐ ┌───┐ ┌───┐┌───┐ ┌──────┐┌───┐┌────┐┌───┐ ┌───┐┌───┐┌───┐│ │├───┴┐├───┬┘├───┤ ├───┴┐ ├───┤├───┤ ├───┬┘├───┤ └───┘└───┘└────┘└───┘└───┘└───┘└───┘│ │└───┘└────┘└───┘ └───┘└───┘└───┘└────┘└───┘└───┘└───┘└────┘»
q_1: ┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├─┤ S ├─────────────┤ ├────────────┤1 ├┤ S ├┤ √X ├┤ S ├─┤ S ├─┤ S ├┤ S ├──────────────────────────────────────┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ S ├┤ Ecr ├┤ √X ├┤ S ├─┤ S ├─┤ √X ├─┤ S ├┤ S ├─┤ S ├─┤ S ├─────────────────────────────────────┤ ├───────────────────────────────────────────────────────────»
├───┤├────┤├───┤ ├───┤├───┤├────┤├───┤ ├───┤┌──────┐┌───┐│ │┌───┐ ┌───┐ │ Ecr │├───┤├────┤├───┤ ├───┤ ├───┤├───┤┌────┐┌───┐ ┌───┐┌───┐┌────┐┌───┐┌───┐│ Ecr │├───┤├───┬┘├───┴┐├───┤├───┤└───┘│ │├───┬┘├───┤ ├───┤┌┴────┴┐├───┤├───┴┐├───┤ ├───┤ ┌───┐┌───┐┌────┐┌───┐ │ Ecr │ »
q_2: ┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤1 ├┤ S ├┤ ├┤ S ├─┤ S ├─┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤1 ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├─────┤1 ├┤ S ├─┤ S ├─┤ S ├┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├───────────────┤ ├───────────────────────────────────────────────────────────»
├───┤├───┬┘├───┴┐├───┤├───┤├───┬┘├───┤ └───┘│ Ecr │├───┤│ Ecr │├───┴┐├───┤ └┬───┬─┘├───┤├───┬┘├───┴┐├───┤ ├───┤├───┤├────┤├───┤ ├───┤└───┘└────┘└───┘└───┘└──────┘└───┘└───┘ └────┘└───┘└───┘ └──────┘└───┘ └───┘ └───┘│ │└───┘└────┘└───┘ └───┘ └───┘└───┘└────┘└───┘ │ │┌───┐┌───┐ ┌────┐┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐ »
q_3: ┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤ S ├─┤ S ├──────┤0 ├┤ S ├┤ ├┤ √X ├┤ S ├──┤ S ├──┤ S ├┤ S ├─┤ √X ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├─┤ S ├──────────────────────────────────────────────────────────────────────────────────────┤ ├───────────────────────────────────────────────────────────┤1 ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├───────────»
├───┤├───┴┐├───┬┘├───┤├───┤├───┤ └───┘ └──────┘└───┘│ │└────┘└───┘ └───┘ └───┘└───┘ └────┘└───┘ └───┘└───┘└────┘└───┘ └───┘ │ Ecr │ └──────┘└───┘└───┘ └────┘└───┘└───┘└───┘└────┘└───┘└───┘ »
q_4: ┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ S ├─────────────────────────┤ ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────»
├───┤├───┬┘├───┤ └───┘└───┘└───┘ │ │┌───┐ ┌────┐ ┌───┐ ┌───┐┌───┐ ┌───┐ ┌────┐┌───┐┌───┐┌───┐ ┌────┐┌───┐┌───┐ │ │┌───┐┌───┐ ┌───┐ ┌────┐┌───┐┌───┐ »
q_5: ┤ S ├┤ S ├─┤ S ├─────────────────────────────────────────┤0 ├┤ S ├─┤ √X ├─┤ S ├──┤ S ├┤ S ├─┤ S ├─┤ √X ├┤ S ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├─────────────────────────────────────────────────────────────────────────────────┤1 ├┤ S ├┤ S ├─┤ S ├─┤ √X ├┤ S ├┤ S ├─────────────────────────────────────────────────────────────────────────────────────────────»
└───┘└───┘ └───┘ └──────┘└───┘ └────┘ └───┘ └───┘└───┘ └───┘ └────┘└───┘└───┘└───┘ └────┘└───┘└───┘ └──────┘└───┘└───┘ └───┘ └────┘└───┘└───┘ »
« ┌───┐┌───┐┌──────┐┌───┐┌───┐ ┌────┐┌───┐┌───┐ ┌──────┐┌───┐ ┌───┐ ┌───┐ ┌──────┐┌───┐ ┌────┐┌───┐ ┌───┐┌───┐ ┌───┐┌────┐┌───┐┌───┐┌───┐┌────┐ ┌───┐ ┌───┐ ┌──────┐┌───┐┌────┐┌───┐┌───┐┌───┐ ┌───┐┌────┐┌───┐ »
«q_0: ┤ S ├┤ S ├┤1 ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├─────┤1 ├┤ S ├─┤ S ├─┤ S ├─────┤0 ├┤ S ├─┤ √X ├┤ S ├─┤ S ├┤ S ├─┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ √X ├─┤ S ├──┤ S ├──────┤1 ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├─┤ S ├┤ √X ├┤ S ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────»
« └───┘└───┘│ Ecr │├───┤├───┴┐├───┬┘├───┤├───┤┌───┐│ │├───┴┐├───┤ ├───┤┌───┐│ │├───┴┐├───┬┘├───┤ ├───┤├───┴┐├───┤├───┬┘└───┘└───┘└───┘└────┘ └───┘ └───┘ │ │└───┘└────┘└───┘└───┘└───┘ └───┘└────┘└───┘ ┌──────┐┌───┐┌───┐ ┌────┐┌───┐┌───┐┌──────┐┌───┐ ┌───┐ ┌───┐┌──────┐┌───┐ ┌────┐┌───┐┌───┐ ┌───┐┌───┐ »
«q_1: ──────────┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ S ├┤ ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ ├┤ √X ├┤ S ├─┤ S ├─┤ S ├┤ √X ├┤ S ├┤ S ├─────────────────────────────────────────┤ ├───────────────────────────────────────────────────────────────────────────┤1 ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤1 ├┤ S ├─┤ S ├─┤ S ├┤0 ├┤ S ├─┤ √X ├┤ S ├┤ S ├─┤ S ├┤ S ├─»
« └──────┘└───┘└────┘└───┘ └───┘└───┘└───┘│ │└────┘└───┘ └───┘└───┘│ │└────┘└───┘ └───┘ └───┘└────┘└───┘└───┘ │ │ │ │└───┘└───┘ └────┘└───┘└───┘│ │└───┘ └───┘ └───┘│ │└───┘ └────┘└───┘└───┘ └───┘└───┘ »
«q_2: ──────────────────────────────────────────────────┤ Ecr ├──────────────────────┤ ├────────────────────────────────────────────────────────────────────────────────┤ ├───────────────────────────────────────────────────────────────────────────┤ ├───────────────────────────┤ Ecr ├─────────────────┤ ├──────────────────────────────────»
« │ │ │ Ecr │ ┌──────┐┌───┐┌───┐ │ Ecr │┌───┐ │ │ │ │┌───┐ ┌────┐┌───┐│ Ecr │┌───┐ ┌───┐ ┌───┐┌────┐┌───┐┌───┐ »
«q_3: ──────────────────────────────────────────────────┤ ├──────────────────────┤ ├─────────────────────────────────────────────────────────────┤1 ├┤ S ├┤ S ├─┤ ├┤ S ├──────────────────────────────────────────────────────────────────────┤ Ecr ├───────────────────────────┤0 ├┤ S ├─┤ √X ├┤ S ├┤ ├┤ S ├─┤ S ├─┤ S ├┤ √X ├┤ S ├┤ S ├─»
« │ │┌───┐ ┌────┐┌───┐┌───┐│ │┌───┐ ┌───┐ ┌────┐┌───┐┌───┐ ┌───┐┌────┐┌───┐┌───┐┌───┐┌───┐ │ Ecr │├───┤├───┴┐│ │├───┤┌───┐ ┌───┐┌───┐┌────┐┌───┐┌───┐ ┌───┐┌────┐┌───┐ ┌───┐ │ │ └──────┘└───┘ └────┘└───┘│ │├───┤ ├───┴┐├───┤├───┬┘├───┤├───┴┐»
«q_4: ──────────────────────────────────────────────────┤0 ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤ ├┤ S ├─┤ S ├─┤ √X ├┤ S ├┤ S ├─┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├─┤0 ├┤ S ├┤ √X ├┤ ├┤ S ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├─┤ S ├┤ √X ├┤ S ├─┤ S ├───────────────┤ ├────────────────────────────────────────────────────┤1 ├┤ S ├─┤ √X ├┤ S ├┤ S ├─┤ S ├┤ √X ├»
« └──────┘└───┘ └────┘└───┘└───┘│ │├───┤ ├───┴┐├───┬┘├───┤├───┤ ├───┤└────┘└───┘└───┘└───┘└───┘ └──────┘└───┘└────┘│ │├───┤├───┴┐├───┤├───┤├───┬┘├───┤├───┴┐├───┤├───┬┘├───┴┐├───┤┌───┐┌───┐┌───┐│ │┌───┐┌────┐┌───┐ ┌───┐┌───┐ ┌───┐ ┌────┐┌───┐ ┌───┐└┬───┬─┘├───┴┐├───┬┘├───┤├───┤ ├───┤└────┘»
«q_5: ────────────────────────────────────────────────────────────────────────────────┤1 ├┤ S ├─┤ √X ├┤ S ├─┤ S ├┤ S ├─┤ S ├──────────────────────────────────────────────┤0 ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├─┤ S ├┤ √X ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├─┤ S ├──┤ √X ├┤ S ├─┤ S ├─┤ S ├──┤ √X ├┤ S ├─┤ S ├┤ S ├─┤ S ├──────»
« └──────┘└───┘ └────┘└───┘ └───┘└───┘ └───┘ └──────┘└───┘└────┘└───┘└───┘└───┘ └───┘└────┘└───┘└───┘ └────┘└───┘└───┘└───┘└───┘└──────┘└───┘└────┘└───┘ └───┘└───┘ └───┘ └────┘└───┘ └───┘ └───┘ └────┘└───┘ └───┘└───┘ └───┘ »
« »
«q_0: ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────»
« ┌────┐┌───┐ ┌───┐ ┌───┐┌───┐ ┌────┐┌───┐ ┌───┐┌───┐ ┌───┐ ┌────┐┌───┐ »
«q_1: ┤ √X ├┤ S ├──┤ S ├──┤ S ├┤ S ├─┤ √X ├┤ S ├─┤ S ├┤ S ├─┤ S ├──┤ √X ├┤ S ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────»
« └────┘└───┘ └───┘ └───┘└───┘ └────┘└───┘ └───┘└───┘ └───┘ └────┘└───┘ »
«q_2: ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────»
« ┌───┐ ┌────┐ ┌───┐ ┌───┐┌───┐ ┌───┐ ┌────┐┌───┐┌───┐┌──────┐┌───┐ ┌───┐ ┌───┐ ┌──────┐┌───┐┌────┐┌───┐ ┌───┐┌───┐┌───┐┌────┐┌───┐ »
«q_3: ┤ S ├─┤ √X ├─┤ S ├──┤ S ├┤ S ├─┤ S ├─┤ √X ├┤ S ├┤ S ├┤1 ├┤ S ├─┤ S ├─┤ S ├───────────────────────────┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────»
« ├───┤ ├───┬┘┌┴───┴─┐├───┤├───┤ ├───┤ └────┘└───┘└───┘│ Ecr │├───┤ ├───┴┐├───┤┌───┐ ┌───┐┌───┐┌────┐┌───┐│ │├───┤├───┬┘├───┴┐├───┤├───┤├───┤├───┬┘├───┴┐┌───┐┌───┐┌──────┐┌───┐┌───┐ ┌───┐ ┌──────┐┌───┐┌────┐┌───┐┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐┌──────┐┌───┐┌───┐ ┌────┐┌───┐┌───┐»
«q_4: ┤ S ├─┤ S ├─┤1 ├┤ S ├┤ S ├─┤ S ├─────────────────┤0 ├┤ S ├─┤ √X ├┤ S ├┤ S ├─┤ S ├┤ S ├┤ √X ├┤ S ├┤ Ecr ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├┤1 ├┤ S ├┤ S ├─┤ S ├────────────────────────────────────────────────────┤0 ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤1 ├┤ S ├┤ S ├─┤ √X ├┤ S ├┤ S ├»
« └───┘ └───┘ │ Ecr │├───┤├───┴┐├───┤ ┌───┐ ┌───┐┌───┐└┬────┬┘├───┤ ├───┬┘├───┤├───┴┐├───┤├───┤└────┘└───┘│ │├───┤├───┤ ├───┬┘├───┤└───┘└───┘└───┘ └────┘└───┘└───┘│ Ecr │├───┤├───┴┐├───┤┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐│ Ecr │├───┤├───┬┘├───┤└───┘└───┘└───┘└────┘└───┘└───┘└───┘└────┘└───┘└───┘│ Ecr │├───┤├───┴┐├───┬┘├───┤├───┤»
«q_5: ────────────┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├─┤ S ├┤ S ├─┤ √X ├─┤ S ├─┤ S ├─┤ S ├┤ √X ├┤ S ├┤ S ├───────────┤1 ├┤ S ├┤ S ├─┤ S ├─┤ S ├────────────────────────────────┤0 ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤1 ├┤ S ├┤ S ├─┤ S ├────────────────────────────────────────────────────┤0 ├┤ S ├┤ √X ├┤ S ├─┤ S ├┤ S ├»
« └──────┘└───┘└────┘└───┘ └───┘ └───┘└───┘ └────┘ └───┘ └───┘ └───┘└────┘└───┘└───┘ └──────┘└───┘└───┘ └───┘ └───┘ └──────┘└───┘└────┘└───┘└───┘└───┘└───┘└────┘└───┘└───┘└───┘└────┘└───┘└───┘└──────┘└───┘└───┘ └───┘ └──────┘└───┘└────┘└───┘ └───┘└───┘»
«
«q_0: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
«
«q_1: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
«
«q_2: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
«
«q_3: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
« ┌──────┐┌───┐┌────┐┌───┐
«q_4: ───────────────────────────────┤1 ├┤ S ├┤ √X ├┤ S ├────────────────────────────────────────────────────────────────────
« ┌───┐┌────┐┌───┐┌───┐┌───┐┌───┐│ Ecr │├───┤├────┤├───┤┌───┐┌───┐┌───┐┌────┐┌───┐┌───┐┌────┐┌───┐┌───┐┌───┐┌───┐┌────┐┌───┐
«q_5: ┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤0 ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ √X ├┤ S ├┤ S ├┤ S ├┤ S ├┤ √X ├┤ S ├
« └───┘└────┘└───┘└───┘└───┘└───┘└──────┘└───┘└────┘└───┘└───┘└───┘└───┘└────┘└───┘└───┘└────┘└───┘└───┘└───┘└───┘└────┘└───┘
looool
@chriseclectic - do you allow only s and sx as basis gates? or also other gates, e.g. sdg, sxdg, x, z, y?
@ShellyGarion it should be up to the user, "sx", "s"
would be minimal, but they could include other clifford basis gates for efficiency in synthesis if you didnt want long circuits like @jakelishman's example with the basis transpilator without optimization.
Essentially any spanning set of our standard Clifford gates should work (i think minimal ones would all have 3 gates from ecr/cx/cz/cy
+ h/sx/sxdg
+ s/sdg
).
For example I might use ["ecr", "sx", "s", "sdg", "z", "id"]
if I wanted to have a circuit that has same shape as transpiling to ["ecr", "rz", "sx"]
basis would, but then replaces each rz with a clifford for the 4 valid clifford angles (mod 2pi).
I added the suggested equivalences to the equivalence library in #9913. As mentioned above, this may produce many redundant S gates, so it may be better to have all clifford z-rotations ["s", "sdg", "z", "id"]
in the basis gates.
Environment
What is happening?
Transpiling a Clifford circuit to a clifford basis containing "sx", but not "h" raises a Transpiler error, similar for transpiling with "ecr" instead of "cx" for the two qubit Clifford gate.
How can we reproduce the issue?
Running
is ok, but running
raises
also raise similar exceptions.
What should happen?
SX should be a valid basis gates for clifford synthesis since it is equivalent to Sdg.H.Sdg. Similar for ECR which can be written as CX + 1 qubit cliffords
Any suggestions?
I'm not sure how clifford synthesis is implemented these days, but it should be updated to support "sx" and "ecr" since they are our native device gate.
My motivation for this is I would like to mimic device transpilation on a clifford simulator by transpiling to a Clifford-equivalent ECR/CX + RZ SX basis, where all the RZ gates would be replaced by either I, Z, Sdg, or S for clifford circuits.