tensorcircuit.templates.measurements#

Shortcuts for measurement patterns on circuit

tensorcircuit.templates.measurements.any_local_measurements(c: tensorcircuit.circuit.Circuit, structures: Any, onehot: bool = False, reuse: bool = True) Any[源代码]#

This measurements pattern is specifically suitable for vmap. Parameterize the local Pauli string to be measured.

Example

c = tc.Circuit(3)
c.X(0)
c.cnot(0, 1)
c.H(-1)
basis = tc.backend.convert_to_tensor(np.array([3, 3, 1]))
z0, z1, x2 = tc.templates.measurements.parameterized_local_measurements(
    c, structures=basis, onehot=True
)
# -1, -1, 1
参数
  • c (Circuit) -- The circuit to be measured

  • structures (Tensor) -- parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if onehot is False and [nwires] if onehot is True.

  • onehot (bool, optional) -- defaults to False. If set to be True, structures will first go through onehot procedure.

  • reuse (bool, optional) -- reuse the wavefunction when computing the expectations, defaults to be True

返回

The expectation value of given Pauli string by the tensor structures.

返回类型

Tensor

tensorcircuit.templates.measurements.any_measurements(c: tensorcircuit.circuit.Circuit, structures: Any, onehot: bool = False, reuse: bool = False) Any[源代码]#

This measurements pattern is specifically suitable for vmap. Parameterize the Pauli string to be measured.

Example

c = tc.Circuit(3)
c.rx(0, theta=1.0)
c.cnot(0, 1)
c.cnot(1, 2)
c.ry(2, theta=-1.0)

z0x2 = c.expectation([tc.gates.z(), [0]], [tc.gates.x(), [2]])
z0x2p1 = tc.templates.measurements.parameterized_measurements(
    c, tc.array_to_tensor(np.array([3, 0, 1])), onehot=True
)
z0x2p2 = tc.templates.measurements.parameterized_measurements(
    c,
    tc.array_to_tensor(np.array([[0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]])),
    onehot=False,
)
np.testing.assert_allclose(z0x2, z0x2p1)
np.testing.assert_allclose(z0x2, z0x2p2)
参数
  • c (Circuit) -- The circuit to be measured

  • structures (Tensor) -- parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if onehot is False and [nwires] if onehot is True.

  • onehot (bool, optional) -- defaults to False. If set to be True, structures will first go through onehot procedure.

  • reuse (bool, optional) -- reuse the wavefunction when computing the expectations, defaults to be False

返回

The expectation value of given Pauli string by the tensor structures.

返回类型

Tensor

tensorcircuit.templates.measurements.heisenberg_measurements(c: tensorcircuit.circuit.Circuit, g: Any, hzz: float = 1.0, hxx: float = 1.0, hyy: float = 1.0, hz: float = 0.0, hx: float = 0.0, hy: float = 0.0, reuse: bool = True) Any[源代码]#

Evaluate Heisenberg energy expectation, whose Hamiltonian is defined on the lattice graph g as follows: (e are edges in graph g where e1 and e2 are two nodes for edge e and v are nodes in graph g)

\[H = \sum_{e\in g} w_e (h_{xx} X_{e1}X_{e2} + h_{yy} Y_{e1}Y_{e2} + h_{zz} Z_{e1}Z_{e2}) + \sum_{v\in g} (h_x X_v + h_y Y_v + h_z Z_v)\]
Example

g = tc.templates.graphs.Line1D(n=5)
c = tc.Circuit(5)
c.X(0)
energy = tc.templates.measurements.heisenberg_measurements(c, g) # 1
参数
  • c (Circuit) -- Circuit to be measured

  • g (Graph) -- Lattice graph defining Heisenberg Hamiltonian

  • hzz (float, optional) -- [description], defaults to 1.0

  • hxx (float, optional) -- [description], defaults to 1.0

  • hyy (float, optional) -- [description], defaults to 1.0

  • hz (float, optional) -- [description], defaults to 0.0

  • hx (float, optional) -- [description], defaults to 0.0

  • hy (float, optional) -- [description], defaults to 0.0

  • reuse (bool, optional) -- [description], defaults to True

返回

Value of Heisenberg energy

返回类型

Tensor

tensorcircuit.templates.measurements.mpo_expectation(c: tensorcircuit.circuit.Circuit, mpo: tensorcircuit.quantum.QuOperator) Any[源代码]#

Evaluate expectation of operator mpo defined in QuOperator MPO format with the output quantum state from circuit c.

参数
返回

a real and scalar tensor of shape [] as the expectation value

返回类型

Tensor

tensorcircuit.templates.measurements.operator_expectation(c: tensorcircuit.circuit.Circuit, hamiltonian: Any) Any[源代码]#

Evaluate Hamiltonian expectation where hamiltonian can be dense matrix, sparse matrix or MPO.

参数
  • c (Circuit) -- The circuit whose output state is used to evaluate the expectation

  • hamiltonian (Tensor) -- Hamiltonian matrix in COO_sparse_matrix form

返回

a real and scalar tensor of shape [] as the expectation value

返回类型

Tensor

tensorcircuit.templates.measurements.parameterized_local_measurements(c: tensorcircuit.circuit.Circuit, structures: Any, onehot: bool = False, reuse: bool = True) Any#

This measurements pattern is specifically suitable for vmap. Parameterize the local Pauli string to be measured.

Example

c = tc.Circuit(3)
c.X(0)
c.cnot(0, 1)
c.H(-1)
basis = tc.backend.convert_to_tensor(np.array([3, 3, 1]))
z0, z1, x2 = tc.templates.measurements.parameterized_local_measurements(
    c, structures=basis, onehot=True
)
# -1, -1, 1
参数
  • c (Circuit) -- The circuit to be measured

  • structures (Tensor) -- parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if onehot is False and [nwires] if onehot is True.

  • onehot (bool, optional) -- defaults to False. If set to be True, structures will first go through onehot procedure.

  • reuse (bool, optional) -- reuse the wavefunction when computing the expectations, defaults to be True

返回

The expectation value of given Pauli string by the tensor structures.

返回类型

Tensor

tensorcircuit.templates.measurements.parameterized_measurements(c: tensorcircuit.circuit.Circuit, structures: Any, onehot: bool = False, reuse: bool = False) Any#

This measurements pattern is specifically suitable for vmap. Parameterize the Pauli string to be measured.

Example

c = tc.Circuit(3)
c.rx(0, theta=1.0)
c.cnot(0, 1)
c.cnot(1, 2)
c.ry(2, theta=-1.0)

z0x2 = c.expectation([tc.gates.z(), [0]], [tc.gates.x(), [2]])
z0x2p1 = tc.templates.measurements.parameterized_measurements(
    c, tc.array_to_tensor(np.array([3, 0, 1])), onehot=True
)
z0x2p2 = tc.templates.measurements.parameterized_measurements(
    c,
    tc.array_to_tensor(np.array([[0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]])),
    onehot=False,
)
np.testing.assert_allclose(z0x2, z0x2p1)
np.testing.assert_allclose(z0x2, z0x2p2)
参数
  • c (Circuit) -- The circuit to be measured

  • structures (Tensor) -- parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if onehot is False and [nwires] if onehot is True.

  • onehot (bool, optional) -- defaults to False. If set to be True, structures will first go through onehot procedure.

  • reuse (bool, optional) -- reuse the wavefunction when computing the expectations, defaults to be False

返回

The expectation value of given Pauli string by the tensor structures.

返回类型

Tensor

tensorcircuit.templates.measurements.sparse_expectation(c: tensorcircuit.circuit.Circuit, hamiltonian: Any) Any[源代码]#

Evaluate Hamiltonian expectation where hamiltonian is kept in sparse matrix form to save space

参数
  • c (Circuit) -- The circuit whose output state is used to evaluate the expectation

  • hamiltonian (Tensor) -- Hamiltonian matrix in COO_sparse_matrix form

返回

a real and scalar tensor of shape [] as the expectation value

返回类型

Tensor

tensorcircuit.templates.measurements.spin_glass_measurements(c: tensorcircuit.circuit.Circuit, g: Any, reuse: bool = True) Any[源代码]#

Compute spin glass energy defined on graph g expectation for output state of the circuit c. The Hamiltonian to be evaluated is defined as (first term is determined by node weights while the second term is determined by edge weights of the graph):

\[H = \sum_{v\in g} w_v Z_v + \sum_{e\in g} w_e Z_{e1} Z_{e2}\]
Example

import networkx as nx

# building the lattice graph for spin glass Hamiltonian
g = nx.Graph()
g.add_node(0, weight=1)
g.add_node(1, weight=-1)
g.add_node(2, weight=1)
g.add_edge(0, 1, weight=-1)
g.add_edge(1, 2, weight=-1)

c = tc.Circuit(3)
c.X(1)
energy = tc.templates.measurements.spin_glass_measurements(c, g)
print(energy) # 5.0
参数
  • c (Circuit) -- The quantum circuit

  • g (Graph) -- The graph for spin glass Hamiltonian definition

  • reuse (bool, optional) -- Whether measure the circuit with reusing the wavefunction, defaults to True

返回

The spin glass energy expectation value

返回类型

Tensor