Skip to content

Commit

Permalink
minor fixup spex backend (#381)
Browse files Browse the repository at this point in the history
* import error in spex backend
* spex-backend: error in hadamard compiling
* test_gradients: samplers/simulators missmatch
  • Loading branch information
kottmanj authored Mar 5, 2025
1 parent ef2e2df commit c5d51d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/tequila/simulators/simulator_spex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from tequila.wavefunction.qubit_wavefunction import QubitWaveFunction
from tequila.utils import TequilaException
from tequila.hamiltonian import PauliString
from tequila.circuit._gates_impl import ExponentialPauliGateImpl, QGateImpl, RotationGateImpl, QubitHamiltonian, QubitExcitationImpl
from tequila.circuit._gates_impl import ExponentialPauliGateImpl, QGateImpl, RotationGateImpl, QubitHamiltonian
from tequila.circuit.gates import QubitExcitationImpl
from tequila import BitNumbering


Expand Down Expand Up @@ -218,15 +219,28 @@ def add_basic_gate(self, gate, circuit, *args, **kwargs):
self.add_basic_gate(sub_gate, circuit, *args, **kwargs)

elif isinstance(gate, QGateImpl):
# Convert standard gates to Pauli rotations
for ps in gate.make_generator(include_controls=True).paulistrings:
angle = numpy.pi * ps.coeff
if self.angle_threshold is not None and abs(angle) < self.angle_threshold:
continue
exp_term = spex_tequila.ExpPauliTerm()
exp_term.pauli_map = dict(ps.items())
exp_term.angle = angle
circuit.append(exp_term)
if gate.name.lower() in ["x","y","z"]:
# Convert standard gates to Pauli rotations
for ps in gate.make_generator(include_controls=True).paulistrings:
angle = numpy.pi * ps.coeff
if self.angle_threshold is not None and abs(angle) < self.angle_threshold:
continue
exp_term = spex_tequila.ExpPauliTerm()
exp_term.pauli_map = dict(ps.items())
exp_term.angle = angle
circuit.append(exp_term)
elif gate.name.lower() in ["h", "hadamard"]:
assert len(gate.target)==1
target = gate.target[0]
for ps in ["-0.25*Y({q})", "Z({q})", "0.25*Y({q})"]:
ps = QubitHamiltonian(ps.format(q=gate.target[0])).paulistrings[0]
angle = numpy.pi * ps.coeff
exp_term = spex_tequila.ExpPauliTerm()
exp_term.pauli_map = dict(ps.items())
exp_term.angle = angle
circuit.append(exp_term)
else:
raise TequilaSpexException("{} not supported. Only x,y,z,h".format(gate.name.lower()))

else:
raise TequilaSpexException(f"Unsupported gate object type: {type(gate)}. "
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_gradient_UY_HX(simulator, angle_value, controlled, assume_real, silent=
print("cos(angle)=", numpy.cos(angle()))


@pytest.mark.parametrize("simulator", simulators)
@pytest.mark.parametrize("simulator", samplers)
@pytest.mark.parametrize("controlled", [False, True])
@pytest.mark.parametrize("assume_real", [False, True])
@pytest.mark.parametrize("angle_value", numpy.random.uniform(0.0, 2.0 * numpy.pi, 1))
Expand Down

0 comments on commit c5d51d6

Please sign in to comment.