|
19 | 19 | import sympy
|
20 | 20 | from cirq_google.api import v2
|
21 | 21 | from cirq_google.ops import InternalGate
|
| 22 | +from cirq.qis import CliffordTableau |
22 | 23 |
|
23 | 24 | SUPPORTED_FUNCTIONS_FOR_LANGUAGE: Dict[Optional[str], FrozenSet[str]] = {
|
24 | 25 | '': frozenset(),
|
@@ -455,3 +456,45 @@ def internal_gate_from_proto(
|
455 | 456 | num_qubits=int(msg.num_qubits),
|
456 | 457 | **gate_args,
|
457 | 458 | )
|
| 459 | + |
| 460 | + |
| 461 | +def clifford_tableau_arg_to_proto( |
| 462 | + value: CliffordTableau, *, out: Optional[v2.program_pb2.CliffordTableau] = None |
| 463 | +): |
| 464 | + """Writes an CliffordTableau object into an CliffordTableau proto. |
| 465 | + Args: |
| 466 | + value: The gate to encode. |
| 467 | + arg_function_language: The language to use when encoding functions. If |
| 468 | + this is set to None, it will be set to the minimal language |
| 469 | + necessary to support the features that were actually used. |
| 470 | + out: The proto to write the result into. Defaults to a new instance. |
| 471 | + Returns: |
| 472 | + The proto that was written into. |
| 473 | + """ |
| 474 | + msg = v2.program_pb2.CliffordTableau() if out is None else out |
| 475 | + msg.num_qubits = value.n |
| 476 | + msg.initial_state = value.initial_state |
| 477 | + msg.xs.extend(value.xs.flatten()) |
| 478 | + msg.rs.extend(value.rs.flatten()) |
| 479 | + msg.zs.extend(value.zs.flatten()) |
| 480 | + return msg |
| 481 | + |
| 482 | + |
| 483 | +def clifford_tableau_from_proto( |
| 484 | + msg: v2.program_pb2.CliffordTableau, arg_function_language: str |
| 485 | +) -> CliffordTableau: |
| 486 | + """Extracts a CliffordTableau object from a CliffordTableau proto. |
| 487 | + Args: |
| 488 | + msg: The proto containing a serialized value. |
| 489 | + arg_function_language: The `arg_function_language` field from |
| 490 | + `Program.Language`. |
| 491 | + Returns: |
| 492 | + The deserialized InternalGate object. |
| 493 | + """ |
| 494 | + return CliffordTableau( |
| 495 | + num_qubits=msg.num_qubits, |
| 496 | + initial_state=msg.initial_state, |
| 497 | + rs=np.array(msg.rs, dtype=bool) if msg.rs else None, |
| 498 | + xs=np.array(msg.xs, dtype=bool).reshape((2 * msg.num_qubits, -1)) if msg.xs else None, |
| 499 | + zs=np.array(msg.zs, dtype=bool).reshape((2 * msg.num_qubits, -1)) if msg.zs else None, |
| 500 | + ) |
0 commit comments