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[source]#
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
- Parameters
c (Circuit) – The circuit to be measured
structures (Tensor) – parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if
onehotis False and [nwires] ifonehotis 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
- Returns
The expectation value of given Pauli string by the tensor
structures.- Return type
Tensor
- tensorcircuit.templates.measurements.any_measurements(c: tensorcircuit.circuit.Circuit, structures: Any, onehot: bool = False, reuse: bool = False) Any[source]#
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)
- Parameters
c (Circuit) – The circuit to be measured
structures (Tensor) – parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if
onehotis False and [nwires] ifonehotis 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
- Returns
The expectation value of given Pauli string by the tensor
structures.- Return type
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[source]#
Evaluate Heisenberg energy expectation, whose Hamiltonian is defined on the lattice graph
gas follows: (e are edges in graphgwhere e1 and e2 are two nodes for edge e and v are nodes in graphg)\[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
- Parameters
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
- Returns
Value of Heisenberg energy
- Return type
Tensor
- tensorcircuit.templates.measurements.mpo_expectation(c: tensorcircuit.circuit.Circuit, mpo: tensorcircuit.quantum.QuOperator) Any[source]#
Evaluate expectation of operator
mpodefined inQuOperatorMPO format with the output quantum state from circuitc.- Parameters
c (Circuit) – The circuit for the output state
mpo (QuOperator) – MPO operator
- Returns
a real and scalar tensor of shape [] as the expectation value
- Return type
Tensor
- tensorcircuit.templates.measurements.operator_expectation(c: tensorcircuit.circuit.Circuit, hamiltonian: Any) Any[source]#
Evaluate Hamiltonian expectation where
hamiltoniancan be dense matrix, sparse matrix or MPO.- Parameters
c (Circuit) – The circuit whose output state is used to evaluate the expectation
hamiltonian (Tensor) – Hamiltonian matrix in COO_sparse_matrix form
- Returns
a real and scalar tensor of shape [] as the expectation value
- Return type
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
- Parameters
c (Circuit) – The circuit to be measured
structures (Tensor) – parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if
onehotis False and [nwires] ifonehotis 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
- Returns
The expectation value of given Pauli string by the tensor
structures.- Return type
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)
- Parameters
c (Circuit) – The circuit to be measured
structures (Tensor) – parameter tensors determines what Pauli string to be measured, shape is [nwires, 4] if
onehotis False and [nwires] ifonehotis 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
- Returns
The expectation value of given Pauli string by the tensor
structures.- Return type
Tensor
- tensorcircuit.templates.measurements.sparse_expectation(c: tensorcircuit.circuit.Circuit, hamiltonian: Any) Any[source]#
Evaluate Hamiltonian expectation where
hamiltonianis kept in sparse matrix form to save space- Parameters
c (Circuit) – The circuit whose output state is used to evaluate the expectation
hamiltonian (Tensor) – Hamiltonian matrix in COO_sparse_matrix form
- Returns
a real and scalar tensor of shape [] as the expectation value
- Return type
Tensor
- tensorcircuit.templates.measurements.spin_glass_measurements(c: tensorcircuit.circuit.Circuit, g: Any, reuse: bool = True) Any[source]#
Compute spin glass energy defined on graph
gexpectation for output state of the circuitc. 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
- Parameters
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
- Returns
The spin glass energy expectation value
- Return type
Tensor