Skip to content

Commit

Permalink
Fix incorrect signage in qDRIFT synthesis (Qiskit#11321)
Browse files Browse the repository at this point in the history
* Enable saving signs in qDRIFT trotterization

* Add release note

* Fix signs in evolution

* Fix formatting issues

* Perserve signs when doing qDRIFT evolution

* Fix spaces in qdrift.py

* Update test_qdrift_evolution

* Add reno release note

* Fix linting

* Update fix-qdrift-evolution-bceb9c4f182ab0f5.yaml

---------

Co-authored-by: Shelly Garion <[email protected]>
  • Loading branch information
TheBabu and ShellyGarion authored Mar 21, 2024
1 parent e0ddf72 commit f713c78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 1 addition & 3 deletions qiskit/synthesis/evolution/qdrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def synthesize(self, evolution):
size=(num_gates,),
p=weights / lambd,
)
# Update the coefficients of sampled_ops
self.sampled_ops = [(op, evolution_time) for op, coeff in self.sampled_ops]

# pylint: disable=cyclic-import
from qiskit.circuit.library.pauli_evolution import PauliEvolutionGate
Expand All @@ -94,7 +92,7 @@ def synthesize(self, evolution):
insert_barriers=self.insert_barriers, atomic_evolution=self.atomic_evolution
)
evolution_circuit = PauliEvolutionGate(
sum(SparsePauliOp(op) for op, coeff in self.sampled_ops),
sum(SparsePauliOp(np.sign(coeff) * op) for op, coeff in self.sampled_ops),
time=evolution_time,
synthesis=lie_trotter,
).definition
Expand Down
3 changes: 3 additions & 0 deletions releasenotes/notes/fix-qdrift-evolution-bceb9c4f182ab0f5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fixes:
- |
Fix incorrect implemention of `qDRIFT`, negative coeffients of the Hamiltonian are now added back whereas they were always forced to be positive.
20 changes: 14 additions & 6 deletions test/python/circuit/library/test_evolution_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,25 @@ def test_qdrift_manual(self, op, time, reps, sampled_ops):

def test_qdrift_evolution(self):
"""Test QDrift on an example."""
op = 0.1 * (Z ^ Z) + (X ^ I) + (I ^ X) + 0.2 * (X ^ X)
op = 0.1 * (Z ^ Z) - 3.2 * (X ^ I) - 1.0 * (I ^ X) + 0.2 * (X ^ X)
reps = 20
qdrift = PauliEvolutionGate(
op, time=0.5 / reps, synthesis=QDrift(reps=reps, seed=self.seed)
).definition
exact = scipy.linalg.expm(-0.5j * op.to_matrix()).dot(np.eye(4)[0, :])
time = 0.12
num_samples = 300
qdrift_energy = []

def energy(evo):
return Statevector(evo).expectation_value(op.to_matrix())

self.assertAlmostEqual(energy(exact), energy(qdrift), places=2)
for i in range(num_samples):
qdrift = PauliEvolutionGate(
op, time=time, synthesis=QDrift(reps=reps, seed=self.seed + i)
).definition

qdrift_energy.append(energy(qdrift))

exact = scipy.linalg.expm(-1j * time * op.to_matrix()).dot(np.eye(4)[0, :])

self.assertAlmostEqual(energy(exact), np.average(qdrift_energy), places=2)

def test_passing_grouped_paulis(self):
"""Test passing a list of already grouped Paulis."""
Expand Down

0 comments on commit f713c78

Please sign in to comment.