mindquantum.core.circuit.add_prefix

View Source On Gitee
mindquantum.core.circuit.add_prefix(circuit_fn, prefix: str)[source]

Add a prefix on the parameter of a parameterized quantum circuit or a parameterized quantum operator.

(a function that can generate a parameterized quantum circuit).

Parameters
  • circuit_fn (Union[Circuit, FunctionType, MethodType]) – A quantum circuit, or a function that can generate a quantum circuit.

  • prefix (str) – The prefix you want to add to every parameters.

Returns

Circuit or a function that can generate a Circuit.

Raises
  • TypeError – If prefix is not a string.

  • TypeErrorcircuit_fn is not a Circuit or can not return a Circuit.

Examples

>>> from mindquantum.algorithm.library import qft
>>> from mindquantum.core.circuit import add_prefix
>>> from mindquantum import RX, H, Circuit
>>> u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])
>>> u1 = u(0)
>>> u2 = add_prefix(u1, 'ansatz')
>>> u3 = add_prefix(u, 'ansatz')
>>> u3 = u3(0)
>>> u2
      ┏━━━┓ ┏━━━━━━━━━━━━━━┓
q0: ──┨ H ┠─┨ RX(ansatz_a) ┠───
      ┗━━━┛ ┗━━━━━━━━━━━━━━┛
>>> u3
      ┏━━━┓ ┏━━━━━━━━━━━━━━┓
q0: ──┨ H ┠─┨ RX(ansatz_a) ┠───
      ┗━━━┛ ┗━━━━━━━━━━━━━━┛