Skip to content

Commit

Permalink
Merge pull request #4 from garrison/kwargs
Browse files Browse the repository at this point in the history
Add kwargs passing to `_gen_quimb_gates`
  • Loading branch information
kevinsung authored Sep 24, 2024
2 parents 1ad74f5 + b6e72f8 commit ff8fd84
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions src/qiskit_quimb/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,87 +49,93 @@ def quimb_gates(circuit: QuantumCircuit) -> list[quimb.tensor.Gate]:
return gates


def _gen_quimb_gates(op: Instruction, qubits: list[int]) -> Iterator[quimb.tensor.Gate]:
def _gen_quimb_gates(
op: Instruction, qubits: list[int], **kwargs
) -> Iterator[quimb.tensor.Gate]:
"""Convert a Qiskit gate to quimb gates."""
match op.name:
case "barrier":
pass
case "ccx":
yield quimb.tensor.Gate("CCX", params=[], qubits=qubits)
yield quimb.tensor.Gate("CCX", params=[], qubits=qubits, **kwargs)
case "ccz":
yield quimb.tensor.Gate("CCZ", params=[], qubits=qubits)
yield quimb.tensor.Gate("CCZ", params=[], qubits=qubits, **kwargs)
case "cp":
(theta,) = op.params
yield quimb.tensor.Gate("CU1", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("CU1", params=[theta], qubits=qubits, **kwargs)
case "cx":
yield quimb.tensor.Gate("CX", params=[], qubits=qubits)
yield quimb.tensor.Gate("CX", params=[], qubits=qubits, **kwargs)
case "cy":
yield quimb.tensor.Gate("CY", params=[], qubits=qubits)
yield quimb.tensor.Gate("CY", params=[], qubits=qubits, **kwargs)
case "cz":
yield quimb.tensor.Gate("CZ", params=[], qubits=qubits)
yield quimb.tensor.Gate("CZ", params=[], qubits=qubits, **kwargs)
case "h":
yield quimb.tensor.Gate("H", params=[], qubits=qubits)
yield quimb.tensor.Gate("H", params=[], qubits=qubits, **kwargs)
case "id":
yield quimb.tensor.Gate("IDEN", params=[], qubits=qubits)
yield quimb.tensor.Gate("IDEN", params=[], qubits=qubits, **kwargs)
case "iswap":
yield quimb.tensor.Gate("ISWAP", params=[], qubits=qubits)
yield quimb.tensor.Gate("ISWAP", params=[], qubits=qubits, **kwargs)
case "measure":
raise ValueError(
"Encountered a measurement gate, which is not allowed. "
"Remove the measurements from your circuit and try again."
)
case "p":
(theta,) = op.params
yield quimb.tensor.Gate("U1", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("U1", params=[theta], qubits=qubits, **kwargs)
case "rx":
(theta,) = op.params
yield quimb.tensor.Gate("RX", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("RX", params=[theta], qubits=qubits, **kwargs)
case "rxx":
(theta,) = op.params
yield quimb.tensor.Gate("RXX", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("RXX", params=[theta], qubits=qubits, **kwargs)
case "ryy":
(theta,) = op.params
yield quimb.tensor.Gate("RYY", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("RYY", params=[theta], qubits=qubits, **kwargs)
case "rzz":
(theta,) = op.params
yield quimb.tensor.Gate("RZZ", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("RZZ", params=[theta], qubits=qubits, **kwargs)
case "ry":
(theta,) = op.params
yield quimb.tensor.Gate("RY", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("RY", params=[theta], qubits=qubits, **kwargs)
case "rz":
(theta,) = op.params
yield quimb.tensor.Gate("RZ", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("RZ", params=[theta], qubits=qubits, **kwargs)
case "s":
yield quimb.tensor.Gate("S", params=[], qubits=qubits)
yield quimb.tensor.Gate("S", params=[], qubits=qubits, **kwargs)
case "sdg":
yield quimb.tensor.Gate("SDG", params=[], qubits=qubits)
yield quimb.tensor.Gate("SDG", params=[], qubits=qubits, **kwargs)
case "swap":
yield quimb.tensor.Gate("SWAP", params=[], qubits=qubits)
yield quimb.tensor.Gate("SWAP", params=[], qubits=qubits, **kwargs)
case "t":
yield quimb.tensor.Gate("T", params=[], qubits=qubits)
yield quimb.tensor.Gate("T", params=[], qubits=qubits, **kwargs)
case "tdg":
yield quimb.tensor.Gate("TDG", params=[], qubits=qubits)
yield quimb.tensor.Gate("TDG", params=[], qubits=qubits, **kwargs)
case "u1":
(theta,) = op.params
yield quimb.tensor.Gate("U1", params=[theta], qubits=qubits)
yield quimb.tensor.Gate("U1", params=[theta], qubits=qubits, **kwargs)
case "u2":
(phi, lam) = op.params
yield quimb.tensor.Gate("U2", params=[phi, lam], qubits=qubits)
yield quimb.tensor.Gate("U2", params=[phi, lam], qubits=qubits, **kwargs)
case "u3":
(theta, phi, lam) = op.params
yield quimb.tensor.Gate("U3", params=[theta, phi, lam], qubits=qubits)
yield quimb.tensor.Gate(
"U3", params=[theta, phi, lam], qubits=qubits, **kwargs
)
case "x":
yield quimb.tensor.Gate("X", params=[], qubits=qubits)
yield quimb.tensor.Gate("X", params=[], qubits=qubits, **kwargs)
case "xx_plus_yy":
theta, beta = op.params
phi = beta + 0.5 * math.pi
a, b = qubits
yield quimb.tensor.Gate("RZ", params=[phi], qubits=[a])
yield quimb.tensor.Gate("GIVENS", params=[0.5 * theta], qubits=[a, b])
yield quimb.tensor.Gate("RZ", params=[-phi], qubits=[a])
yield quimb.tensor.Gate("RZ", params=[phi], qubits=[a], **kwargs)
yield quimb.tensor.Gate(
"GIVENS", params=[0.5 * theta], qubits=[a, b], **kwargs
)
yield quimb.tensor.Gate("RZ", params=[-phi], qubits=[a], **kwargs)
case "y":
yield quimb.tensor.Gate("Y", params=[], qubits=qubits)
yield quimb.tensor.Gate("Y", params=[], qubits=qubits, **kwargs)
case "z":
yield quimb.tensor.Gate("Z", params=[], qubits=qubits)
yield quimb.tensor.Gate("Z", params=[], qubits=qubits, **kwargs)
case _:
raise ValueError(f"Unsupported gate: {op.name}.")

0 comments on commit ff8fd84

Please sign in to comment.