Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

circuit.library.fourier_checking needs testing #4348

Closed
1ucian0 opened this issue Apr 29, 2020 · 8 comments · Fixed by #4624
Closed

circuit.library.fourier_checking needs testing #4348

1ucian0 opened this issue Apr 29, 2020 · 8 comments · Fixed by #4624
Assignees
Labels
good first issue Good for newcomers type: enhancement It's working, but needs polishing type: qa Issues and PRs that relate to testing and code quality

Comments

@1ucian0
Copy link
Member

1ucian0 commented Apr 29, 2020

The object FourierChecking (in qiskit/circuit/library/fourier_checking.py) needs testing. No line is currently coveraged.

@1ucian0 1ucian0 added type: enhancement It's working, but needs polishing good first issue Good for newcomers type: qa Issues and PRs that relate to testing and code quality labels Apr 29, 2020
@1ucian0 1ucian0 added this to To do in Test, CI, installation, and QA via automation Apr 29, 2020
@Dozzco
Copy link

Dozzco commented Apr 29, 2020

Hi I would like to work on this issue, please may I be assigned?

@1ucian0
Copy link
Member Author

1ucian0 commented Apr 30, 2020

Assigned! Let me know if you need any help.

@Dozzco
Copy link

Dozzco commented May 1, 2020

Thanks, I'll get on it now!

@Dozzco
Copy link

Dozzco commented May 4, 2020

Sorry I'm taking so long, have had a lot of stuff going on, Testing the FourierChecking object now

@mantcep
Copy link
Contributor

mantcep commented Jun 3, 2020

Hi!

Would it be fine for me to pick this issue up if it is not being worked on at the moment? I am working on a similar issue (#4349) at the moment, so I think I would have enough background to pick this one up.

@mantcep
Copy link
Contributor

mantcep commented Jun 24, 2020

Hi again!

I looked into this briefly and came up with the following:

@ddt
class TestFourierCheckingLibrary(QiskitTestCase):
    """Test the Fourier Checking circuit."""

    def assertFourierCheckingIsCorrect(self, f, g, fc_circuit):
        """Assert that the Fourier Checking circuit produces the correct matrix."""

        simulated = Operator(fc_circuit)
        
        num_qubits = int(np.log2(len(f)))
        
        # create Hadamard matrix, re-use approach from TestMCMT
        h_i = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]])
        h_tot = np.array([1])
        for _ in range(num_qubits):
            h_tot = np.kron(h_tot, h_i)
        
        f_mat = np.diag(f)
        g_mat = np.diag(g)
        
        expected = np.linalg.multi_dot([h_tot, g_mat, h_tot, f_mat, h_tot])
        expected = Operator(expected)
        
        self.assertTrue(expected.equiv(simulated))
    
    @data(
        ([1, -1, -1, -1], [1, 1, -1, -1])
    )
    def test_fourier_checking(self, f, g):
        """Test if the Fourier Checking circuit produces the correct matrix."""
        fc_circuit = FourierChecking(f, g)
        self.assertFourierCheckingIsCorrect(f, g, fc_circuit)
    
    @data(
        ([1, -1, -1, -1], [1, 1, -1])
    )
    def test_invalid_input_raises(self, f, g):
        """Test that invalid input truth tables raise an error."""
        with self.assertRaises(CircuitError):
            FourierChecking(f, g)

Would something like this be good for this test? It is a simple check based on calculating the circuit matrix multiplying Hadamard and diagonal truth table matrices. I assumed this straightforward check should be fine. Additionally, I thought it would make sense to add a check that an error is raised with an invalid input.

If you would think this looks good, I would be more than glad to make a pull request with the test. If someone else is working on this issue maybe this piece of code could be helpful for others.
If this code does not really make sense, feel free to ignore this comment 🙂

Thank you!

@Cryoris
Copy link
Contributor

Cryoris commented Jun 26, 2020

Thanks for the contribution @mantcep, please open a PR for this! 👍

@mantcep
Copy link
Contributor

mantcep commented Jun 27, 2020

Thank you for the feedback, @Cryoris! I have created the PR and already got a review on that 🙂

@mergify mergify bot closed this as completed in #4624 Sep 5, 2020
Test, CI, installation, and QA automation moved this from To do to Done Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers type: enhancement It's working, but needs polishing type: qa Issues and PRs that relate to testing and code quality
Development

Successfully merging a pull request may close this issue.

4 participants