Skip to content

Commit

Permalink
Merge pull request #47 from thisac/fix-cond-not-propagating
Browse files Browse the repository at this point in the history
Fix conditional not propagating with parametric ops
  • Loading branch information
thisac authored Feb 2, 2024
2 parents a8cc8a3 + 7584e6d commit 54f6485
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dwave/gate/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def append(self, operation: Operation) -> None:
# if a parametric operation is called within a non-parameteric circuit, all variables
# should be replaced by their corresponding parameter values; eval does that
eval = getattr(operation, "eval", None)
operation = eval() if eval else operation
operation = eval(inplace=True) if eval else operation

for q in operation.qubits or []:
if q not in self.qubits:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Updates the circuit so that conditional parametric gates are evaluated in place when appended to
the active circuit, fixing an issue where changes to these gates post application are not
propagated to the circuit.
43 changes: 43 additions & 0 deletions tests/test_simulator/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ def test_conditional_op_multiple_qubits_false(self):
assert np.allclose(circuit.state, expected)
assert x.is_blocked

# assert that the returned op is the circuit op
assert x is circuit.circuit[-1]

def test_conditional_op_multiple_qubits_true(self):
"""Test simulating a circuit with a multiple conditional ops (true)."""
circuit = Circuit(2, 2)
Expand All @@ -227,6 +230,46 @@ def test_conditional_op_multiple_qubits_true(self):
assert np.allclose(circuit.state, expected)
assert not x.is_blocked

# assert that the returned op is the circuit op
assert x is circuit.circuit[-1]

def test_conditional_op_parametric_false(self):
"""Test simulating a circuit with a conditional parametric op (false)."""
circuit = Circuit(2, 1)

with circuit.context as (q, c):
ops.Measurement(q[0]) | c[0] # state is |00>
rx = ops.RX(np.pi, q[1]).conditional(c[0])

simulate(circuit)
# should NOT apply X on qubit 1 leaving state in |00>
expected = np.array([1, 0, 0, 0])

assert np.allclose(circuit.state, expected)
assert rx.is_blocked

# assert that the returned op is the circuit op
assert rx is circuit.circuit[-1]

def test_conditional_op_parametric_true(self):
"""Test simulating a circuit with a conditional parametric op (true)."""
circuit = Circuit(2, 1)

with circuit.context as (q, c):
ops.X(q[0])
ops.Measurement(q[0]) | c[0] # state is |10>
rx = ops.RX(np.pi, q[1]).conditional(c[0])

simulate(circuit)
# should apply RX(pi) on qubit 1 changing state to |11> (with extra global phase)
expected = np.array([0, 0, 0, -1j])

assert np.allclose(circuit.state, expected)
assert not rx.is_blocked

# assert that the returned op is the circuit op
assert rx is circuit.circuit[-1]

def test_bell_state_measurement(self):
"""Test that measurement gives one of two correct states and sets the resulting
state vector correctly for the Bell state."""
Expand Down

0 comments on commit 54f6485

Please sign in to comment.