Skip to content

Commit

Permalink
Reorganized tests of old non-native PL operations, added test for Qub…
Browse files Browse the repository at this point in the history
…itUnitary transformation (ch gate)
  • Loading branch information
antalszava committed Nov 20, 2019
1 parent 5ebc929 commit 7bb7641
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,19 @@ def test_one_qubit_parametrized_operations_supported_by_pennylane(self, recorder
single_wire = [0]
angle = 0.3333

phi = 0.3
lam = 0.4
theta = 0.2

q_reg = QuantumRegister(1)
qc = QuantumCircuit(q_reg)

qc.u1(angle, single_wire)
qc.rx(angle, single_wire)
qc.ry(angle, single_wire)
qc.rz(angle, single_wire)
qc.u2(phi, lam, [0])
qc.u3(phi, lam, theta, [0])

quantum_circuit = load(qc)
with recorder:
Expand All @@ -327,6 +333,16 @@ def test_one_qubit_parametrized_operations_supported_by_pennylane(self, recorder
assert recorder.queue[3].params == [angle]
assert recorder.queue[3].wires == single_wire

assert recorder.queue[4].name == 'U2'
assert len(recorder.queue[4].params) == 2
assert recorder.queue[4].params == [0.3, 0.4]
assert recorder.queue[4].wires == [0]

assert recorder.queue[5].name == 'U3'
assert len(recorder.queue[5].params) == 3
assert recorder.queue[5].params == [0.3, 0.4, 0.2]
assert recorder.queue[5].wires == [0]

def test_two_qubit_operations_supported_by_pennylane(self, recorder):
"""Tests loading a circuit with the two-qubit operations supported by PennyLane."""

Expand Down Expand Up @@ -396,6 +412,7 @@ def test_three_qubit_operations_supported_by_pennylane(self, recorder):

qc = QuantumCircuit(3, 1)
qc.cswap(*three_wires)
qc.ccx(*three_wires)
quantum_circuit = load(qc)

with recorder:
Expand All @@ -405,6 +422,10 @@ def test_three_qubit_operations_supported_by_pennylane(self, recorder):
assert recorder.queue[0].params == []
assert recorder.queue[0].wires == three_wires

assert recorder.queue[1].name == 'Toffoli'
assert len(recorder.queue[1].params) == 0
assert recorder.queue[1].wires == three_wires

def test_wires_two_different_quantum_registers(self, recorder):
"""Tests loading a circuit with the three-qubit operations supported by PennyLane."""

Expand Down Expand Up @@ -466,23 +487,14 @@ def test_wires_pass_different_wires_than_for_circuit(self, recorder):
assert recorder.queue[0].params == []
assert recorder.queue[0].wires == three_wires

def test_operations_transformed_into_qubit_unitary(self, recorder):
"""Tests loading a circuit with operations that can be converted,
but not natively supported by PennyLane."""
def test_operations_sdg_and_tdg(self, recorder):
"""Tests loading a circuit with the operations Sdg and Tdg gates."""

qc = QuantumCircuit(3, 1)

phi = 0.3
lam = 0.4
theta = 0.2

qc.sdg([0])
qc.tdg([0])

qc.u2(phi, lam, [0])
qc.u3(phi, lam, theta, [0])
qc.ccx(0, 1, 2)

quantum_circuit = load(qc)
with recorder:
quantum_circuit()
Expand All @@ -495,19 +507,22 @@ def test_operations_transformed_into_qubit_unitary(self, recorder):
assert len(recorder.queue[1].params) == 0
assert recorder.queue[1].wires == [0]

assert recorder.queue[2].name == 'U2'
assert len(recorder.queue[2].params) == 2
assert recorder.queue[2].params == [0.3, 0.4]
assert recorder.queue[2].wires == [0]
def test_operation_transformed_into_qubit_unitary(self, recorder):
"""Tests loading a circuit with operations that can be converted,
but not natively supported by PennyLane."""

assert recorder.queue[3].name == 'U3'
assert len(recorder.queue[3].params) == 3
assert recorder.queue[3].params == [0.3, 0.4, 0.2]
assert recorder.queue[3].wires == [0]
qc = QuantumCircuit(3, 1)

qc.ch([0], [1])

assert recorder.queue[4].name == 'Toffoli'
assert len(recorder.queue[4].params) == 0
assert recorder.queue[4].wires == [0, 1, 2]
quantum_circuit = load(qc)
with recorder:
quantum_circuit()

assert recorder.queue[0].name == 'QubitUnitary'
assert len(recorder.queue[0].params) == 1
assert np.array_equal(recorder.queue[0].params[0], ex.CHGate().to_matrix())
assert recorder.queue[0].wires == [0, 1]

def test_quantum_circuit_error_by_passing_wrong_parameters(self, recorder):
"""Tests the load method for a QuantumCircuit raises a QiskitError,
Expand Down

0 comments on commit 7bb7641

Please sign in to comment.