Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

522 lines
19KB

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This module implements Anders and Briegel's method for fast simulation of Clifford circuits.
  5. """
  6. from __future__ import absolute_import
  7. from __future__ import print_function
  8. import itertools as it
  9. import json, random
  10. from . import qi, clifford, util
  11. import abp
  12. from .stabilizer import Stabilizer
  13. import requests
  14. from six.moves import range
  15. from six.moves import zip
  16. class GraphState(object):
  17. """
  18. This is the main class used to model stabilizer states.
  19. Internally it uses the same dictionary-of-dictionaries data structure as ``networkx``.
  20. """
  21. def __init__(self, data=(), vop="identity"):
  22. """ Construct a ``GraphState``
  23. :param data: An iterable of nodes used to construct the graph, or an integer -- the number of nodes, or a ``nx.Graph``.
  24. :param vop: The default VOP for new qubits. Setting ``vop="identity"`` initializes qubits in :math:`|+\\rangle`. Setting ``vop="hadamard"`` initializes qubits in :math:`|0\\rangle`.
  25. """
  26. self.adj = {}
  27. self.node = {}
  28. self.url = None
  29. try:
  30. # Cloning from a networkx graph
  31. self.adj = data.adj.copy()
  32. self.node = data.node.copy()
  33. for key, value in list(self.node.items()):
  34. self.node[key]["vop"] = data.node[
  35. key].get("vop", clifford.identity)
  36. except AttributeError:
  37. try:
  38. # Provided with a list of node names?
  39. for n in data:
  40. self._add_node(n, vop=vop)
  41. except TypeError:
  42. # Provided with an integer?
  43. for n in range(data):
  44. self._add_node(n, vop=vop)
  45. def add_node(self, *args, **kwargs):
  46. """ Add a node """
  47. self._add_node(self, *args, **kwargs)
  48. def _del_node(self, node):
  49. """ Remove a node. TODO: this is a hack right now. """
  50. if not node in self.node:
  51. return
  52. del self.node[node]
  53. for k in self.adj[node]:
  54. del self.adj[k][node]
  55. del self.adj[node]
  56. def del_qubit(self, node):
  57. """ Remove a qubit. TODO: this is a hack right now. """
  58. self._del_node(node)
  59. def _add_node(self, node, **kwargs):
  60. """ Add a node.
  61. By default, nodes are initialized with ``vop=``:math:`I`, i.e. they are in the :math:`|+\\rangle` state.
  62. """
  63. if node in self.node:
  64. print(("Warning: node {} already exists".format(node)))
  65. return
  66. default = kwargs.get("default", "identity")
  67. self.adj[node] = {}
  68. self.node[node] = {}
  69. kwargs["vop"] = clifford.by_name[
  70. str(kwargs.get("vop", "identity"))] # TODO: ugly
  71. self.node[node].update(kwargs)
  72. def add_qubit(self, name, **kwargs):
  73. """ Add a qubit to the state.
  74. :param name: The name of the node, e.g. ``9``, ``start``.
  75. :type name: Any hashable type
  76. :param kwargs: Any extra node attributes
  77. By default, qubits are initialized in the :math:`|0\\rangle` state. Provide the optional ``vop`` argument to set the initial state.
  78. Example of using node attributes ::
  79. >>> g._add_node(0, label="fred", position=(1,2,3))
  80. >>> g.node[0]["label"]
  81. fred
  82. """
  83. kwargs["vop"] = clifford.by_name[
  84. str(kwargs.get("vop", "hadamard"))] # TODO: ugly
  85. self._add_node(name, **kwargs)
  86. def act_circuit(self, circuit):
  87. """ Run many gates in one call.
  88. :param circuit: An iterable containing tuples of the form ``(operation, node)``. If ``operation`` is a name for a local operation (e.g. ``6``, ``hadamard``) then that operation is performed on ``node``. If ``operation`` is ``cz`` then a CZ is performed on the two nodes in ``node``.
  89. Example (makes a Bell pair)::
  90. >>> g.act_circuit([("hadamard", 0), ("hadamard", 1), ("cz", (0, 1))])
  91. """
  92. for node, operation in circuit:
  93. if operation == "cz":
  94. self.act_cz(*node)
  95. else:
  96. self.act_local_rotation(node, operation)
  97. def act_czs(self, *pairs):
  98. """ Shorthand to act many CZs """
  99. for a, b in pairs:
  100. self.act_cz(a, b)
  101. def _add_edge(self, v1, v2, data={}):
  102. """ Add an edge between two vertices """
  103. self.adj[v1][v2] = data
  104. self.adj[v2][v1] = data
  105. def _del_edge(self, v1, v2):
  106. """ Delete an edge between two vertices """
  107. del self.adj[v1][v2]
  108. del self.adj[v2][v1]
  109. def has_edge(self, v1, v2):
  110. """ Test existence of an edge between two vertices """
  111. return v2 in self.adj[v1]
  112. def _toggle_edge(self, v1, v2):
  113. """ Toggle an edge between two vertices """
  114. if self.has_edge(v1, v2):
  115. self._del_edge(v1, v2)
  116. else:
  117. self._add_edge(v1, v2)
  118. def edgelist(self):
  119. """ Describe a graph as an edgelist # TODO: inefficient """
  120. edges = set(tuple(sorted((i, n)))
  121. for i, v in list(self.adj.items())
  122. for n in v)
  123. return tuple(edges)
  124. def remove_vop(self, node, avoid):
  125. """ Attempts to remove the vertex operator on a particular qubit.
  126. :param node: The node whose vertex operator should be reduced to the identity.
  127. :param avoid: We will try to leave this node alone during the process (if possible).
  128. """
  129. others = set(self.adj[node]) - {avoid}
  130. if abp.DETERMINISTIC:
  131. swap_qubit = min(others) if others else avoid
  132. else:
  133. swap_qubit = others.pop() if others else avoid
  134. for v in reversed(clifford.decompositions[self.node[node]["vop"]]):
  135. if v == "x":
  136. self.local_complementation(node)
  137. else:
  138. self.local_complementation(swap_qubit)
  139. def local_complementation(self, v):
  140. """ As defined in LISTING 1 of Anders & Briegel """
  141. for i, j in it.combinations(self.adj[v], 2):
  142. self._toggle_edge(i, j)
  143. self.node[v]["vop"] = clifford.times_table[
  144. self.node[v]["vop"], clifford.msqx_h]
  145. for i in self.adj[v]:
  146. self.node[i]["vop"] = clifford.times_table[
  147. self.node[i]["vop"], clifford.sqz_h]
  148. def act_local_rotation(self, node, operation):
  149. """ Act a local rotation on a qubit
  150. :param node: The index of the node to act on
  151. :param operation: The Clifford-group operation to perform. You can use any of the names in the :ref:`Clifford group alias table <clifford>`.
  152. """
  153. rotation = clifford.by_name[str(operation)]
  154. self.node[node]["vop"] = clifford.times_table[
  155. rotation, self.node[node]["vop"]]
  156. def _update_vop(self, v, op):
  157. """ Update a VOP - only used internally"""
  158. rotation = clifford.by_name[str(op)]
  159. self.node[v]["vop"] = clifford.times_table[
  160. self.node[v]["vop"], rotation]
  161. def act_hadamard(self, qubit):
  162. """ Shorthand for ``self.act_local_rotation(qubit, "hadamard")`` """
  163. self.act_local_rotation(qubit, 10)
  164. def _lonely(self, a, b):
  165. """ Is this qubit _lonely ? """
  166. return len(self.adj[a]) > (b in self.adj[a])
  167. def act_cz(self, a, b):
  168. """ Act a controlled-phase gate on two qubits
  169. :param a: The first qubit
  170. :param b: The second qubit
  171. """
  172. if self._lonely(a, b):
  173. self.remove_vop(a, b)
  174. if self._lonely(b, a):
  175. self.remove_vop(b, a)
  176. if self._lonely(a, b) and not clifford.is_diagonal(self.node[a]["vop"]):
  177. self.remove_vop(a, b)
  178. edge = self.has_edge(a, b)
  179. va = self.node[a]["vop"]
  180. vb = self.node[b]["vop"]
  181. new_edge, self.node[a]["vop"], self.node[b]["vop"] = \
  182. clifford.cz_table[int(edge), va, vb]
  183. if new_edge != edge:
  184. self._toggle_edge(a, b)
  185. def measure(self, node, basis, force=None, detail=False, friend=None):
  186. """ Measure in an arbitrary basis
  187. :param node: The name of the qubit to measure.
  188. :param basis: The basis in which to measure.
  189. :param friend: Specify a node to toggle about when performing an :math:`X` measurement.
  190. :type friend: Any neighbour of ``node``.
  191. :type basis: :math:`\in` ``{"px", "py", "pz"}``
  192. :param force: Forces the measurement outcome.
  193. :type force: boolean
  194. :param detail: Get detailed information.
  195. :type detail: boolean
  196. Measurements in quantum mechanics are probabilistic. If you want to force a particular outcome :math:`\in\{0, 1\}`, use ``force``.
  197. You can get more information by setting ``detail=True``, in which case ``measure()`` returns a dictionary with the following keys:
  198. - ``outcome``: the measurement outcome.
  199. - ``determinate``: indicates whether the outcome was determinate or random. For example, measuring :math:`|0\\rangle` in :math:`\sigma_x` always gives a deterministic outcome. ``determinate`` is overridden by ``force`` -- forced outcomes are always determinate.
  200. - ``conjugated_basis``: The index of the measurement operator, rotated by the vertex operator of the measured node, i.e. :math:`U_\\text{vop} \sigma_m U_\\text{vop}^\dagger`.
  201. - ``phase``: The phase of the cojugated basis, :math:`\pm 1`.
  202. - ``node``: The name of the measured node.
  203. - ``force``: The value of ``force``.
  204. """
  205. basis = clifford.by_name[basis]
  206. ha = clifford.conjugation_table[self.node[node]["vop"]]
  207. basis, phase = clifford.conjugate(basis, ha)
  208. # Flip a coin
  209. result = force if force != None else random.choice([0, 1])
  210. # Flip the result if we have negative phase
  211. if phase == -1:
  212. result = not result
  213. if basis == clifford.px:
  214. result, determinate = self._measure_graph_x(node, result, friend)
  215. elif basis == clifford.py:
  216. result, determinate = self._measure_graph_y(node, result)
  217. elif basis == clifford.pz:
  218. result, determinate = self._measure_graph_z(node, result)
  219. else:
  220. raise ValueError("You can only measure in {X,Y,Z}")
  221. # Flip the result if we have negative phase
  222. if phase == -1:
  223. result = not result
  224. if detail:
  225. return {"outcome": int(result),
  226. "determinate": (determinate or force != None),
  227. "conjugated_basis": basis,
  228. "phase": phase,
  229. "node": node,
  230. "force": force}
  231. else:
  232. return int(result)
  233. def measure_x(self, node, force=None, detail=False, friend=None):
  234. """ Measure in the X basis """
  235. return self.measure(node, "px", force, detail, friend)
  236. def measure_y(self, node, force=None, detail=False):
  237. """ Measure in the Y basis """
  238. return self.measure(node, "py", force, detail)
  239. def measure_z(self, node, force=None, detail=False):
  240. """ Measure in the Z basis """
  241. return self.measure(node, "pz", force, detail)
  242. def measure_sequence(self, measurements, forces=None, detail=False):
  243. """ Measures a sequence of Paulis
  244. :param measurements: The sequence of measurements to be made, in the form [(node, basis), ...]
  245. :type force: list of tuples
  246. :param force: Measurements in quantum mechanics are probabilistic. If you want to force a particular outcome, use the ``force``. List outcome force values in same order as measurements
  247. :type force: list
  248. :param detail: Provide detailed information
  249. :type detail: boolean
  250. """
  251. forces = forces if forces != None else [
  252. random.choice([0, 1]) for i in range(len(measurements))]
  253. measurements = list(zip(measurements, forces))
  254. results = []
  255. for (node, basis), force in measurements:
  256. result = self.measure(node, basis, force, detail)
  257. results += [result]
  258. return results
  259. def _toggle_edges(self, a, b):
  260. """ Toggle edges between vertex sets a and b """
  261. # TODO: i'm pretty sure this is just a single-line it.combinations or
  262. # equiv
  263. done = set()
  264. for i, j in it.product(a, b):
  265. if i != j and not (i, j) in done:
  266. done.add((i, j))
  267. done.add((j, i))
  268. self._toggle_edge(i, j)
  269. def _measure_graph_x(self, node, result, friend=None):
  270. """ Measure the bare graph in the X-basis """
  271. if len(self.adj[node]) == 0:
  272. return 0, True
  273. # Pick a friend vertex
  274. if friend == None:
  275. if abp.DETERMINISTIC:
  276. friend = sorted(self.adj[node].keys())[0]
  277. else:
  278. friend = next(iter(self.adj[node].keys()))
  279. else:
  280. assert friend in list(self.adj[node].keys()) # TODO: unnecessary assert
  281. # Update the VOPs. TODO: pretty ugly
  282. if result:
  283. # Do a z on all ngb(vb) \ ngb(v) \ {v}, and some other stuff
  284. self._update_vop(friend, "msqy")
  285. self._update_vop(node, "pz")
  286. for n in set(self.adj[friend]) - set(self.adj[node]) - {node}:
  287. self._update_vop(n, "pz")
  288. else:
  289. # Do a z on all ngb(v) \ ngb(vb) \ {vb}, and sqy on the friend
  290. self._update_vop(friend, "sqy")
  291. for n in set(self.adj[node]) - set(self.adj[friend]) - {friend}:
  292. self._update_vop(n, "pz")
  293. # Toggle the edges. TODO: Yuk. Just awful!
  294. a = set(self.adj[node].keys())
  295. b = set(self.adj[friend].keys())
  296. self._toggle_edges(a, b)
  297. intersection = a & b
  298. for i, j in it.combinations(intersection, 2):
  299. self._toggle_edge(i, j)
  300. for n in a - {friend}:
  301. self._toggle_edge(friend, n)
  302. return result, False
  303. def _measure_graph_y(self, node, result):
  304. """ Measure the bare graph in the Y-basis """
  305. # Do some rotations
  306. for neighbour in self.adj[node]:
  307. self._update_vop(neighbour, "sqz" if result else "msqz")
  308. # A sort of local complementation
  309. vngbh = set(self.adj[node]) | {node}
  310. for i, j in it.combinations(vngbh, 2):
  311. self._toggle_edge(i, j)
  312. # TODO: naming: # lcoS.herm_adjoint() if result else lcoS
  313. self._update_vop(node, 5 if result else 6)
  314. return result, False
  315. def _measure_graph_z(self, node, result):
  316. """ Measure the bare graph in the Z-basis """
  317. # Disconnect
  318. for neighbour in tuple(self.adj[node]):
  319. self._del_edge(node, neighbour)
  320. if result:
  321. self._update_vop(neighbour, "pz")
  322. # Rotate
  323. if result:
  324. self._update_vop(node, "px")
  325. self._update_vop(node, "hadamard")
  326. else:
  327. self._update_vop(node, "hadamard")
  328. return result, False
  329. def order(self):
  330. """ Get the number of qubits """
  331. return len(self.node)
  332. def __str__(self):
  333. """ Represent as a string for quick debugging """
  334. s = ""
  335. for key in sorted(self.node.keys()):
  336. s += "{}: {}\t".format(key,
  337. clifford.get_name(self.node[key]["vop"]))
  338. if self.adj[key]:
  339. s += str(tuple(self.adj[key].keys())).replace(" ", "")
  340. else:
  341. s += "-"
  342. s += "\n"
  343. return s
  344. def to_json(self, stringify=False):
  345. """ Convert the graph to JSON-like form.
  346. :param stringify: JSON keys must be strings, But sometimes it is useful to have a JSON-like object whose keys are tuples.
  347. If you want to dump a graph to disk, do something like this::
  348. >>> import json
  349. >>> with open("graph.json") as f:
  350. json.dump(graph.to_json(True), f)
  351. """
  352. if stringify:
  353. node = {str(key): value for key, value in list(self.node.items())}
  354. adj = {str(key): {str(key): value for key, value in list(ngbh.items())}
  355. for key, ngbh in list(self.adj.items())}
  356. return {"node": node, "adj": adj}
  357. else:
  358. return {"node": self.node, "adj": self.adj}
  359. def from_json(self, data):
  360. """ Construct the graph from JSON data
  361. :param data: JSON data to be read.
  362. """
  363. self.node = data["node"]
  364. self.adj = data["adj"]
  365. def to_state_vector(self):
  366. """ Get the full state vector corresponding to this stabilizer state. Useful for debugging, interface with other simulators.
  367. This method becomes very slow for more than about ten qubits!
  368. The output state is represented as a ``abp.qi.CircuitModel``::
  369. >>> print g.to_state_vector()
  370. |00000❭: 0.18+0.00j
  371. |00001❭: 0.18+0.00j ...
  372. """
  373. if len(self.node) > 15:
  374. raise ValueError("Cannot build state vector: too many qubits")
  375. state = qi.CircuitModel(len(self.node))
  376. mapping = {node: i for i, node in enumerate(sorted(self.node))}
  377. for n in self.node:
  378. state.act_hadamard(mapping[n])
  379. for i, j in self.edgelist():
  380. state.act_cz(mapping[i], mapping[j])
  381. for i, n in list(self.node.items()):
  382. state.act_local_rotation(mapping[i], clifford.unitaries[n["vop"]])
  383. return state
  384. def to_stabilizer(self):
  385. """
  386. Get the stabilizer representation of the state::
  387. >>> print g.to_stabilizer()
  388. 0 1 2 3 100 200
  389. ------------------------------
  390. X Z Z X
  391. Z X Z
  392. Z Z X
  393. - Z Z
  394. X Z
  395. Z X
  396. """
  397. return Stabilizer(self)
  398. def __eq__(self, other):
  399. """ Check equality between GraphStates """
  400. return self.adj == other.adj and self.node == other.node
  401. def copy(self):
  402. """ Make a copy of this graphstate """
  403. g = GraphState()
  404. g.node = self.node.copy()
  405. g.adj = self.adj.copy()
  406. return g
  407. def push(self):
  408. """ Shares the state on the server and displays browser """
  409. if self.url == None:
  410. self.url = requests.get("https://abv.peteshadbolt.co.uk/").url
  411. data = json.dumps(self.to_json(stringify=True))
  412. print(("Shared state to {}".format(self.url)))
  413. return requests.post("{}/graph".format(self.url), data=data)
  414. def pull(self, url=None):
  415. """ Loads the state from the server """
  416. if url:
  417. self.url = url
  418. response = requests.get("{}/graph".format(self.url))
  419. self.from_json(json.loads(response.content))